Compiling a Java D-Bus program

From Apertium
Revision as of 12:55, 19 December 2007 by Wynand.winterbach (talk | contribs) (Moved this out from D-Bus_examples)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Read the guide to installing the Java D-Bus bindings if you don't have the library installed already.

Since both D-Bus and Java use statically typed systems, Java needs to have the D-Bus interface descriptions of the objects it will use available at compile time. The 'CreateInterface' program in the Java D-Bus distribution creates Java interfaces by introspecting D-Bus objects.

We will create a Java interface corresponding to the info.apertium.Info interface and we will store it under the file org/apertium/Info.java. To do this, execute:

CreateInterface org.apertium.info / > org/apertium/Info.java

(and remember to use dbus-launch --exit-with-session if this does not work).

The contents out the newly created file should look something like:

/* File: org/apertium/Info.java */
package org.apertium;
import java.util.List;
import org.freedesktop.dbus.DBusInterface;
public interface Info extends DBusInterface
{

  public String directory();
  public List<List<String>> get_pipeline(String mode);
  public List<String> get_filters(String _type);
  public List<String> modes();
  public String mode_directory();

}

The next step is to create our minimal Java program. Create a file called TestDBus with the following contents:

import org.freedesktop.DBus;
import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;

import org.apertium.Info; // The interface we use to access org.apertium.info/

class TestDBus {

        public static void main(String[] args) throws org.freedesktop.dbus.exceptions.DBusException {
                DBusConnection bus = null;
                Info info = null;

                bus = DBusConnection.getConnection(DBusConnection.SESSION);
                info = bus.getRemoteObject("org.apertium.info", "/", Info.class);

                for (String s : info.modes()) {
                        System.out.println(s);
                }

                bus.disconnect();
        }
}

To compile this program, we need to include the current path on the class path as well as the jar files needed by the Java D-Bus binding. To compile, execute (you should modify the directories to suit your installation):

javac -cp .:/usr/local/share/java/dbus.jar:/usr/local/share/java/unix.jar:\
/usr/local/share/java/debug-disable.jar  TestDBus.java

To run the program, you need to specify the class paths specified above, as well as the JNI code (which installs to /usr/local/lib/jni by default (if you are using TCP instead of UNIX domain sockets for transport, then this won't be needed). You can run the program using:

java -cp .:/usr/local/share/java/dbus.jar:\
/usr/local/share/java/unix.jar:/usr/local/share/java/debug-disable.jar\
-Djava.library.path=/usr/local/lib/jni TestDBus

(and if it does not work, prefix the command with dbus-launch --exit-with-session).

Of course, if you are writing an application, you would hide all of these flags in a wrapper script.