Libvirt Java API usage tutorial in Windows (1)

Source: Internet
Author: User
Tags xen hypervisor

Libvirt (http://libvirt.org/) is a good toolkit for virtualization environment management. The core is implemented in c, but APIs are called in different languages. The official website is described as follows:

 

Libvirt is:
  • A toolkit to interact with the specified alization capabilities of recent versions of Linux (and other OSes), see our project goals for details.
  • Free software available under the GNU Lesser General Public License.
  • A long term stable C API
  • A set of bindings for common ages
  • A cim provider for the DMTF virtualization schema
  • A qmf agent for the AMQP/QPid messaging system
Libvirt supports:
  • The KVM/QEMU Linux hypervisor
  • The Xen hypervisor on Linux and Solaris hosts.
  • The LXC Linux container system
  • The OpenVZ Linux container system
  • The User Mode Linux paravirtualized kernel
  • The VirtualBox hypervisor
  • The VMware ESX and GSX hypervisors
  • The VMware Workstation and Player hypervisors
  • The Microsoft Hyper-V hypervisor
  • Virtual networks using bridging, NAT, VEPA and VN-LINK.
  • Storage on IDE/SCSI/USB disks, fiber rechannel, LVM, iSCSI, NFS and filesystems
Libvirt provides:
  • Remote management using TLS encryption and x509 certificates
  • Remote management authenticating with Kerberos and SASL
  • Local access control using PolicyKit
  • Zero-conf discovery using Avahi multicast-DNS
  • Management of virtual machines, virtual networks and storage
  • Portable client API for Linux, Solaris and Windows

 

Since I am a simple and pure Java programmer, I naturally can only rely on libvirt's Java binding api. As a source code control, I chose to download the source code for verification. The source code git address is as follows: git clone git: // lib+.org/lib+-java.git the author downloaded the source code and directly constructed the Eclipse project, of course, you can use the source code to compile (ant) a jar to depend on: cd libvirt-java
Ant build libvirt also provides Maven Library: Does the http://www.libvirt.org/maven2/ have Maven? Can download Jar package directly from Maven Library: http://www.libvirt.org/maven2/org/libvirt/libvirt/ so many ways, I believe you can always get a copy of libvirt source code or Jar. Because the core of libvirt is written in c, Java API only helps you encapsulate local calls to dynamic link library (dll) files, so what you should do now is install the dll file. The libvirt official website provides scripts for compiling dll files by yourself: MSYS Build script

The easiest way is to useMsys_setupScript, developed by Matthias Bolte. This is actively developed and kept current with libvirt releases:

Https://github.com/photron/msys_setup

However, I did not try this method, because the libvirt official website also provides the installation package for windows:

Experimental installation package

A windows installation package is in development. An experimental version is available here:

Http://libvirt.org/sources/win32_experimental/Libvirt-0.8.8-0.exe

It is not production ready. (Note: It is not a published product)

The installation package not only contains the required dll files, but also provides a convenient and easy-to-use virsh-shell command line tool. You can call all interfaces of libvirt through commands (view, manage virtual machines .), It is very helpful for our development and debugging.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/124U41127-0.jpg "border =" 0 "alt =" "/>

After the installation is complete, you need to specify the jna path to find the dll file for Java API. either of the two methods is to directly set the system environment variables:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/124U4G52-1.jpg "border =" 0 "alt =" "/>

The other method can be dynamically specified in the program. The latter method is flexible and simple, and the test code is written as follows:

 
 
  1. public void testGetXenVMs() { 
  2.         try { 
  3.             System.setProperty("jna.library.path", "D:/Git-Repo/git/libvirt-java/libvirt-java/src/test/java/kubi/coder/"); 
  4.             Connect conn = new Connect("xen+tcp://10.4.55.203/"); 
  5.             System.out.println(conn.nodeInfo().cores); 
  6.             for (String name : conn.listDefinedDomains()) { 
  7.                 System.out.println(name); 
  8.                 if (name != null) { 
  9.                     Domain domain = conn.domainLookupByName(name); 
  10.                     System.out.println(domain.getMaxMemory()); 
  11.                     System.out.println(domain.getUUIDString()); 
  12.                     System.out.println(domain.getInfo().maxMem); 
  13.                     System.out.println(domain.getInfo().state); 
  14.                     System.out.println(conn.listDomains().length); 
  15.                 } 
  16.             } 
  17.         } catch (LibvirtException e) { 
  18.             e.printStackTrace(); 
  19.         } 
  20.     } 

Is the dll still not found? An exception is reported?

 
 
  1. Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'virt' 

It turns out that he is searching for a dll file called virt.

View Source Code:

 
 
  1. Libvirt INSTANCE = (Libvirt) Native.loadLibrary("virt", Libvirt.class); 

Indeed, change the libvirt-0.dll to virt. dll. The result finally came out. Note: Java API encapsulation of libvirt is intuitive and easy to use. The connection class is Connect. After obtaining the connection, you can view and manage the virtual machine environment. I will introduce the Java API in detail later.

 

 

This article is from the "coder" blog and is not reposted!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.