tests: Use the new DO_TEST_CAPS_*() macros
[libvirt/ericb.git] / docs / java.html.in
blob147b11c1666368b718585d6e18e993d4d08a2dda
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE html>
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <body>
5 <h1>Java API bindings</h1>
7 <h2>Presentation</h2>
8 <p>The Java bindings make use of <a href="https://jna.dev.java.net/">JNA</a>
9 to expose the C API in a Java friendly way. The bindings are based on
10 work initiated by Toth Istvan.</p>
12 <h2>Getting it</h2>
13 <p>
14 The latest versions of the libvirt Java bindings can be downloaded from:
15 </p>
17 <ul>
18 <li><a href="ftp://libvirt.org/libvirt/java/">libvirt.org FTP server</a></li>
19 <li><a href="https://libvirt.org/sources/java/">libvirt.org HTTP server</a></li>
20 </ul>
22 <h3>Maven</h3>
23 <p>A maven repository is located at <a href="https://libvirt.org/maven2/">https://libvirt.org/maven2/</a>
24 which you can use to include this in your maven projects.</p>
26 <h2>GIT source repository</h2>
27 <p> The Java bindings code source is now maintained in a <a
28 href="http://git-scm.com/">git</a> repository available on
29 <a href="https://libvirt.org/git/">libvirt.org</a>:
30 </p>
31 <pre>
32 git clone https://libvirt.org/git/libvirt-java.git
33 </pre>
34 <p>
35 It can also be browsed at
36 </p>
37 <pre>
39 <a href="https://libvirt.org/git/?p=libvirt-java.git;a=summary">https://libvirt.org/git/?p=libvirt-java.git;a=summary</a>
40 </pre>
43 <h2>Building</h2>
44 <p>The code is built using ant, and assumes that you have the jna jar installed. Once you have downloaded
45 the code you can build the code with</p>
47 <pre>
49 % cd libvirt-java
50 % ant build
51 </pre>
54 <h2>Content</h2>
55 <p>The bindings are articulated around a few
56 classes in the <code>org/libvirt</code> package, notably the
57 <code>Connect</code>, <code>Domain</code> and <code>Network</code>
58 ones. Functions in the <a href="html/index.html">C API</a>
59 taking <code>virConnectPtr</code>, <code>virDomainPtr</code> or
60 <code>virNetworkPtr</code> as their first argument usually become
61 methods for the classes, their name is just stripped from the
62 virConnect or virDomain(Get) prefix and the first letter gets converted to
63 lower case, for example the C functions:</p>
64 <p>
65 <code>int <a href="html/libvirt-libvirt-domain.html#virConnectNumOfDomains">virConnectNumOfDomains</a>
66 (virConnectPtr conn);</code>
67 </p>
68 <p>
69 <code>int <a href="html/libvirt-libvirt-domain.html#virDomainSetMaxMemory">virDomainSetMaxMemory</a>
70 (virDomainPtr domain, unsigned long memory);</code>
71 </p>
72 <p>become</p>
73 <p>
74 <code>virConn.numOfDomains()</code>
75 </p>
76 <p>
77 <code>virDomain.setMaxMemory(long memory)</code>
78 </p>
79 <p> There is of course some functions where the mapping is less direct
80 and using extra classes to map complex arguments. The <a href="https://libvirt.org/sources/java/javadoc">Javadoc</a> is available online or as
81 part of a separate libvirt-java-javadoc package.</p>
82 <p>So let's look at a simple example inspired from the
83 <code>test.java</code> test found in <code>src</code> in the source tree:</p>
84 <pre>import <span style="color: #0071FF; background-color: #FFFFFF">org.libvirt.*</span>;
85 public class minitest {
86 public static void main(String[] args) {
87 Connect conn=null;
88 try{
89 conn = new <span style="color: #0071FF; background-color: #FFFFFF">Connect</span>("test:///default", true);
90 } catch (<span style="color: #0071FF; background-color: #FFFFFF">LibvirtException</span> e) {
91 System.out.println("exception caught:"+e);
92 System.out.println(e.getError());
94 try{
95 <span style="color: #0071FF; background-color: #FFFFFF">Domain</span> testDomain=conn.<span style="color: #007F00; background-color: #FFFFFF">domainLookupByName</span>("test");
96 System.out.println("Domain:" + testDomain.<span style="color: #E50073; background-color: #FFFFFF">getName</span>() + " id " +
97 testDomain.<span style="color: #E50073; background-color: #FFFFFF">getID</span>() + " running " +
98 testDomain.<span style="color: #E50073; background-color: #FFFFFF">getOSType</span>());
99 } catch (<span style="color: #0071FF; background-color: #FFFFFF">LibvirtException</span> e) {
100 System.out.println("exception caught:"+e);
101 System.out.println(e.getError());
105 </pre>
106 <p>There is not much to comment about it, it really is a straight mapping
107 from the C API, the only points to notice are:</p>
108 <ul>
109 <li>the import of the modules in the <code><span style="color: #0071FF; background-color: #FFFFFF">org.libvirt</span></code> package</li>
110 <li>getting a connection to the hypervisor, in that case using the
111 readonly access to the default test hypervisor.</li>
112 <li>getting an object representing the test domain using <span style="color: #007F00; background-color: #FFFFFF">lookupByName</span></li>
113 <li>if the domain is not found a LibvirtError exception will be raised</li>
114 <li>extracting and printing some information about the domain using
115 various <span style="color: #E50073; background-color: #FFFFFF">methods</span>
116 associated to the Domain class.</li>
117 </ul>
118 <h2>Maven</h2>
119 <p>Up until version 0.4.7 the Java bindings were available from the central maven repository.</p>
120 <p>If you want to use 0.4.8 or higher, please add the following repository to your pom.xml</p>
121 <pre>&lt;repositories&gt;
122 &lt;repository&gt;
123 &lt;id&gt;libvirt-org&lt;/id&gt;
124 &lt;url&gt;https://libvirt.org/maven2&lt;/url&gt;
125 &lt;/repository&gt;
126 &lt;/repositories&gt;</pre>
128 </body>
129 </html>