You need to sign in or sign up before continuing.
java.html.in 5.9 KB
Newer Older
1 2 3 4 5 6
<?xml version="1.0"?>
<html>
  <body>
    <h1>Java API bindings</h1>

<h2>Presentation</h2>
B
Bryan Kearney 已提交
7 8 9
    <p>The Java bindings make use of <a href="https://jna.dev.java.net/">JNA</a>
    to expose the C API in a Java friendly way. The bindings are are based on
    work initiated by Toth Istvan.</p>
10 11 12 13 14 15 16 17 18 19 20

<h2>Getting it</h2>
<p>
  The latest versions of the libvirt Java bindings can be downloaded from:
</p>

<ul>
<li><a href="ftp://libvirt.org/libvirt/java/">libvirt.org FTP server</a></li>
<li><a href="http://libvirt.org/sources/java/">libvirt.org HTTP server</a></li>
</ul>

B
Bryan Kearney 已提交
21 22 23
<h3>Maven</h3>
<p>A maven repository is located at <a href="http://www.libvirt.org/maven2/">http://www.libvirt.org/maven2/</a>
which you can use to include this in your maven projects.</p>
24

B
Bryan Kearney 已提交
25 26 27 28 29 30 31 32 33 34 35 36
<h2>GIT source repository</h2>
<p> The Java bindings code source is now maintained in a <a
href="http://git-scm.com/">git</a> repository available on
<a href="http://libvirt.org/git/">libvirt.org</a>:
</p>
<pre>
git clone git://libvirt.org/libvirt-java.git
</pre>
<p>
It can also be browsed at
</p>
<pre>
37

B
Bryan Kearney 已提交
38 39
<a href="http://libvirt.org/git/?p=libvirt-java.git;a=summary">http://libvirt.org/git/?p=libvirt-java.git;a=summary</a>
</pre>
40

B
Bryan Kearney 已提交
41 42 43 44 45 46
<p></p>
<h2>GIT repository mirror</h2>

<p>
  The source code is also mirrored on <a href="http://www.gitorious.org">gitorious</a>
  to aid in public development. You clone the repo with
47 48 49
</p>

<pre>
B
Bryan Kearney 已提交
50
  git clone git://gitorious.org/libvirt/libvirt-java.git
51 52 53
</pre>

<p>
B
Bryan Kearney 已提交
54
  It can also be browsed at
55 56 57 58
</p>

<pre>

B
Bryan Kearney 已提交
59
  <a href="http://gitorious.org/libvirt/libvirt-java">http://gitorious.org/libvirt/libvirt-java</a>
60 61
</pre>

B
Bryan Kearney 已提交
62 63 64 65 66 67 68 69
<p></p>
<h2>CVS repository access (Deprecated)</h2>

<p> The old source repository uses <a
href="http://ximbiot.com/cvs/cvshome/docs/">CVS</a> and anonymous access
is provided. Prior to accessing the server is it necessary to authenticate
using the password <code>anoncvs</code>. This can be accomplished with
the <code>cvs login</code> command:
70
</p>
B
Bryan Kearney 已提交
71

72
<pre>
B
Bryan Kearney 已提交
73
  # cvs -d :pserver:anoncvs@libvirt.org:2401/data/cvs login
74 75 76
</pre>

<p>
B
Bryan Kearney 已提交
77
  Once authenticated, a checkout can be obtained using
78 79
</p>

B
Bryan Kearney 已提交
80 81
<pre>
  # cvs -d :pserver:anoncvs@libvirt.org:2401/data/cvs co libvirt-java
82 83
</pre>

B
Bryan Kearney 已提交
84 85 86 87
<p></p>
<h2>Building</h2>
<p>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</p>
88 89 90

<pre>

B
Bryan Kearney 已提交
91 92
% cd libvirt-java
% ant build
93 94
</pre>

B
Bryan Kearney 已提交
95

96 97
<h2>Content</h2>
<p>The bindings are articulated around a few
B
Bryan Kearney 已提交
98
classes in the <code>org/libvirt</code> package, notably the
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
<code>Connect</code>, <code>Domain</code> and <code>Network</code>
ones. Functions in the <a href="html/libvirt-libvirt.html">C API</a>
taking <code>virConnectPtr</code>, <code>virDomainPtr</code> or
<code>virNetworkPtr</code> 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:</p>
    <p>
      <code>int <a href="html/libvirt-libvirt.html#virConnectNumOfDomains">virConnectNumOfDomains</a>
(virConnectPtr conn);</code>
    </p>
    <p>
      <code>int <a href="html/libvirt-libvirt.html#virDomainSetMaxMemory">virDomainSetMaxMemory</a>
(virDomainPtr domain, unsigned long memory);</code>
    </p>
    <p>become</p>
    <p>
B
Bryan Kearney 已提交
116
      <code>virConn.numOfDomains()</code>
117 118
    </p>
    <p>
B
Bryan Kearney 已提交
119
      <code>virDomain.setMaxMemory(long memory)</code>
120 121
    </p>
    <p> There is of course some functions where the mapping is less direct
B
Bryan Kearney 已提交
122
and using extra classes to map complex arguments. The <a href="http://libvirt.org/sources/java/javadoc">Javadoc</a> is available online or as
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
part of a separate libvirt-java-javadoc package.</p>
    <p>So let's look at a simple example inspired from the
<code>test.java</code> test found in <code>src</code> in the source tree:</p>
    <pre>import <span style="color: #0071FF; background-color: #FFFFFF">org.libvirt.*</span>;
public class minitest {
    public static void main(String[] args) {
        Connect conn=null;
        try{
            conn = new <span style="color: #0071FF; background-color: #FFFFFF">Connect</span>("test:///default", true);
        } catch (<span style="color: #0071FF; background-color: #FFFFFF">LibvirtException</span> e){
            System.out.println("exception caught:"+e);
            System.out.println(e.getError());
        }
        try{
            <span style="color: #0071FF; background-color: #FFFFFF">Domain</span> testDomain=conn.<span style="color: #007F00; background-color: #FFFFFF">domainLookupByName</span>("test");
            System.out.println("Domain:" + testDomain.<span style="color: #E50073; background-color: #FFFFFF">getName</span>() + " id " +
                               testDomain.<span style="color: #E50073; background-color: #FFFFFF">getID</span>() + " running " +
                               testDomain.<span style="color: #E50073; background-color: #FFFFFF">getOSType</span>());
        } catch (<span style="color: #0071FF; background-color: #FFFFFF">LibvirtException</span> e){
            System.out.println("exception caught:"+e);
            System.out.println(e.getError());
        }
    }
}
</pre>
    <p>There is not much to comment about it, it really is a straight mapping
from the C API, the only points to notice are:</p>
    <ul>
B
Bryan Kearney 已提交
151
      <li>the import of the modules in the <code><span style="color: #0071FF; background-color: #FFFFFF">org.libvirt</span></code> package</li>
152 153 154 155 156 157 158 159 160 161
      <li>getting a connection to the hypervisor, in that case using the
    readonly access to the default test hypervisor.</li>
      <li>getting an object representing the test domain using <span style="color: #007F00; background-color: #FFFFFF">lookupByName</span></li>
      <li>if the domain is not found a LibvirtError exception will be raised</li>
      <li>extracting and printing some information about the domain using
    various <span style="color: #E50073; background-color: #FFFFFF">methods</span>
    associated to the Domain class.</li>
    </ul>
  </body>
</html>