Java API bindings


The Java bindings make use of JNA to expose the C API in a Java friendly way. The bindings are based on work initiated by Toth Istvan.

Getting it

The latest versions of the libvirt Java bindings can be downloaded from:


A maven repository is located at which you can use to include this in your maven projects.

GIT source repository

The Java bindings code source is now maintained in a git repository available on

git clone git://

It can also be browsed at;a=summary

GIT repository mirror

The source code is also mirrored on gitorious to aid in public development. You clone the repo with

  git clone git://

It can also be browsed at

CVS repository access (Deprecated)

The old source repository uses CVS and anonymous access is provided. Prior to accessing the server is it necessary to authenticate using the password anoncvs. This can be accomplished with the cvs login command:

  # cvs -d login

Once authenticated, a checkout can be obtained using

  # cvs -d co libvirt-java


The code is built using ant, and assumes that you have the jna jar installed. Once you have downloaded the code you can build the code with

% cd libvirt-java
% ant build


The bindings are articulated around a few classes in the org/libvirt package, notably the Connect, Domain and Network ones. Functions in the C API taking virConnectPtr, virDomainPtr or virNetworkPtr as their first argument usually become methods for the classes, their name is just stripped from the virConnect or virDomain(Get) prefix and the first letter gets converted to lower case, for example the C functions:

int virConnectNumOfDomains (virConnectPtr conn);

int virDomainSetMaxMemory (virDomainPtr domain, unsigned long memory);



virDomain.setMaxMemory(long memory)

There is of course some functions where the mapping is less direct and using extra classes to map complex arguments. The Javadoc is available online or as part of a separate libvirt-java-javadoc package.

So let's look at a simple example inspired from the test found in src in the source tree:

import org.libvirt.*;
public class minitest {
    public static void main(String[] args) {
        Connect conn=null;
            conn = new Connect("test:///default", true);
        } catch (LibvirtException e){
            System.out.println("exception caught:"+e);
            Domain testDomain=conn.domainLookupByName("test");
            System.out.println("Domain:" + testDomain.getName() + " id " +
                               testDomain.getID() + " running " +
        } catch (LibvirtException e){
            System.out.println("exception caught:"+e);

There is not much to comment about it, it really is a straight mapping from the C API, the only points to notice are:


Up until version 0.4.7 the Java bindings were available from the central maven repository.

If you want to use 0.4.8 or higher, please add the following repository to your pom.xml