Next: 3.3 How the Wrapping Up: 3. Programming GNUstep in Previous: 3.1 Compiling Java Code

Subsections


3.2 Accessing the GNUstep Base Library From Java

3.2.1 A First Example

Accessing the GNUstep Base Library from your Java code is very easy. The GNUstep Base Library classes are made available from Java as part of the gnu.gnustep.base package. If you have correctly installed JIGS, this package has been installed as any other Java package by the GNUstep Makefile package in your GNUstep directory tree. For example, on my system the GNUstep Base Library classes are in:

/usr/GNUstep/System/Libraries/Java/gnu/gnustep/base/
To use the classes, it is enough to import them with the statement:
import gnu.gnustep.base.*;

As a simple example, here is a java program which reads the host name using GNUstep, and prints it out:

import gnu.gnustep.base.*;

class PrintHostName
{ 
  public static void main (String[] args) 
    throws Throwable
  {
    NSProcessInfo process;

    process = NSProcessInfo.processInfo ();
    System.out.println (process.hostName ());
  }
}
We will comment in detail the code in the next section.

The GNUmakefile for this program is the usual, trivial, one:

include $(GNUSTEP_MAKEFILES)/common.make

JAVA_PACKAGE_NAME = PrintHostName
PrintHostName_JAVA_FILES = PrintHostName.java

include $(GNUSTEP_MAKEFILES)/java.make
This is probably a good moment to pause and try out this little program on your system, to get a feeling of how simple and easy the whole thing is.

3.2.2 The General Idea

The previous code example showed the basic idea behind the GNUstep Java Interface: Java and Objective-C are very similar languages, so that Objective-C classes can be exposed as Java classes nearly as they are, with very little (if any) changings in the API.

In this example, the Objective-C class NSProcessInfo provided by the GNUstep Base Library is exposed to Java as the Java class

gnu.gnustep.base.NSProcessInfo
Usually, classes in the same Objective-C library are exposed as classes belonging to the same Java package; for example all classes in the GNUstep Base Library are exposed as part of the gnu.gnustep.base package, and all the classes in the GNUstep Gui Library are exposed as part of the gnu.gnustep.gui package.

Both in Java and Objective-C there are class methods (called static methods in Objective-C) and instance methods. The interface preservers both the name and the role of the Objective-C methods when they are exposed to Java: static methods are made available as class methods with the same name, and instance methods are made available as instance methods with the same name. This is the general plan - but there are exceptions and pitfalls, because the languages differ in some details - we will carefully study these exceptions and details in the next sections.

In the example, the Objective-C static method processInfo is accessible in Java as the class method processInfo; the Objective-C instance method hostName is exposed as a Java method hostName.

The only difference is in the syntax: in Objective-C you would write

NSProcessInfo *process;

process = [NSProcessInfo processInfo];
because this is the Objective-C syntax for method calls; in Java the same code is written as
NSProcessInfo process;

process = NSProcessInfo.processInfo ();
because this is the Java syntax for method calls.

If you want to know exactly which classes and methods are exposed to Java, with which name, and to which Objective-C classes and methods they correspond, you can consult the quick reference documentation for the GNUstep base library wrappers. This is generated automatically by JIGS when it creates the wrappers; if you have installed JIGS correctly, it should be in:

$(GNUSTEP_SYSTEM_ROOT)/Documentation/Developer/Base/Java/Reference/
Warning: this is only a quick reference for the java wrapper, listing classes and methods exposed to Java, and does not document the GNUstep Base Library itself.


Next: 3.3 How the Wrapping Up: 3. Programming GNUstep in Previous: 3.1 Compiling Java Code
Nicola Pero 2001-07-24