Libvirt(http://libvirt.org/)是一個比較不錯的虛擬化環境管理的工具包。核心用c實現,不過提供了不一樣語言的調用API。官網的簡介以下:html
The easiest way is to use the msys_setup script, developed by Matthias Bolte. This is actively developed and kept current with libvirt releases:java
https://github.com/photron/msys_setup node
不過筆者並無嘗試該種方式,由於libvirt官網也提供了windows下的安裝包:git
A windows installation package is in development. An experimental version is available here:程序員
http://libvirt.org/sources/win32_experimental/Libvirt-0.8.8-0.exegithub
It is not production ready.(注:其並非已經發布的產品)shell
該安裝包中不只包含了須要的dll文件,還提供了一個方便好用的virsh-shell 命令行工具,經過命令能夠調用libvirt的全部接口(查看,管理虛擬機等。),對於咱們的開發調試是很是有幫助的。windows
安裝完成後,想讓Java API找到dll文件,還須要指定jna路徑,有兩種方式,一種是直接設置系統環境變量:api
另外一種是可在程序中動態指定,筆者選擇了後者,比較靈活簡單,編寫測試代碼以下:dom
- public void testGetXenVMs() {
- try {
- System.setProperty("jna.library.path", "D:/Git-Repo/git/libvirt-java/libvirt-java/src/test/java/kubi/coder/");
- Connect conn = new Connect("xen+tcp://10.4.55.203/");
- System.out.println(conn.nodeInfo().cores);
- for (String name : conn.listDefinedDomains()) {
- System.out.println(name);
- if (name != null) {
- Domain domain = conn.domainLookupByName(name);
- System.out.println(domain.getMaxMemory());
- System.out.println(domain.getUUIDString());
- System.out.println(domain.getInfo().maxMem);
- System.out.println(domain.getInfo().state);
- System.out.println(conn.listDomains().length);
- }
- }
- } catch (LibvirtException e) {
- e.printStackTrace();
- }
- }
是否是仍是找不到dll?報異常?
- Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'virt'
原來他是搜索叫virt的dll文件。
查看源碼:
- Libvirt INSTANCE = (Libvirt) Native.loadLibrary("virt", Libvirt.class);