guestfs-java(3) Java から libguestfs を使用する方法



GuestFS g = new GuestFS ();
g.add_drive ("disk.img",
new HashMap<String,Object>() {
put ("readonly", Boolean.TRUE);
put ("format", "raw");
g.launch ();


This manual page documents how to call libguestfs from the Java programming language. This page just documents the differences from the C API and gives some examples. If you are not familiar with using libguestfs, you also need to read guestfs(3).


The handle is closed when it is reaped by the garbage collector. Because libguestfs handles include a lot of state, it is also possible to close (and hence free) them explicitly by calling the "close" method.


Errors from libguestfs functions are mapped into the "LibGuestFSException" exception. This has a single parameter which is the error message (a "String").

Calling any method on a closed handle raises the same exception.


The libguestfs event API is fully supported from Java. Create a class which implements the "EventCallback" interface, create an instance of this class, and then call the "GuestFS#set_event_callback" method to register this instance. The "event" method of the class is called when libguestfs generates an event.

For example, this will print all trace events:

 GuestFS g = new GuestFS ();
 g.set_trace (true);
 g.set_event_callback (
   new EventCallback () {
     public void event (long event, int eh,
                        String buffer, long[] array) {
       System.out.println (GuestFS.eventToString (event) +
                           ": " + buffer);
 g.add_drive_ro ("disk.img");
 // etc.

The output looks similar to this:

 EVENT_TRACE: add_drive_ro "disk.img"
 EVENT_TRACE: add_drive_ro = 0
 // etc.


Some methods take an optional map of optional parameters. An example of this is "g.add_drive" which can be called in one of two ways:

 g.add_drive ("disk.img");

or with optional arguments:

 Map<String, Object> optargs =
   new HashMap<String, Object>() {
     put ("readonly", Boolean.TRUE);
     put ("format", "raw");
 g.add_drive ("disk.img", optargs);

For more information on this topic, see ``CALLS WITH OPTIONAL ARGUMENTS'' in guestfs(3).

Optional handle parameters

When creating the handle you can also pass a map of optional parameters:

 Map<String, Object> optargs =
   new HashMap<String, Object>() {
     put ("close_on_exit", Boolean.FALSE);
     put ("environment", Boolean.TRUE);
 GuestFS g = new GuestFS (optargs);

For more information, see ``guestfs_create_flags'' in guestfs(3).


Libguestfs for Java is a Java Native Interface (JNI) extension, supplied in three parts:
The pure Java JAR file which contains several classes, the primary one being "". Upstream, the JAR file contains a version number in the filename, but some Linux distros may rename it without the version number.
The JNI code (written in C). This contains private native functions that interface between Java code and the regular libguestfs C library. You should not call these directly.
The regular libguestfs C library.

To compile your Java program, you need to locate the JAR file and add it to the class path. For example:

 export CLASSPATH=/usr/share/java/libguestfs.jar

To run your Java program, you also need to ensure that the JAR file is on the class path, as well as the path of your program. For example:

 export CLASSPATH=.:/usr/share/java/libguestfs.jar
 java MyProgram

例 1: ディスクイメージの作成


例 2: 仮想マシンのディスクイメージの検査



Richard W.M. Jones ("rjones at redhat dot com")


Copyright (C) 2011-2012 Red Hat Inc.



To get a list of bugs against libguestfs, use this link:

To report a new bug against libguestfs, use this link:

When reporting a bug, please supply:

  • The version of libguestfs.
  • Where you got libguestfs (eg. which Linux distro, compiled from source, etc)
  • Describe the bug accurately and give a way to reproduce it.
  • Run libguestfs-test-tool(1) and paste the complete, unedited output into the bug report.