[prev] [up] [next]

Java Support - Getting Started

This document describes step-by-step how to start using STX:LIBJAVA. You need a running Smalltalk/X environment installed on your machine. For download and installation notes refer to Section Installation & Configuration.

Hello World! Example

Preparing Java classe

In this short example, we will show how to initialize Java support, how load a Java class into Smalltalk/X environment and how to call methods on this class.

First, let's create a simple HelloWorld.java:

  
package stx.libjava.examples;

public class HelloWorld {
  public void sayHello(String name) {
    System.out.println("Hello " + name);
   }
  
  public static void main(String[] args) { 
    new HelloWorld().sayHello(args[0]);
  }
}

Compile it using standard javac:

  javac HelloWorld.java

Let's assume that resulting HelloWorld.class is in directory ~/libjava-example-1/stx/libjava/examples. Therefore, the classpath one would use to run it under standard JVM would be ~/libjava-example-1.

Initializing Java support

In order to run Java code within Smalltalk/X, Java support must be properly initialized. This includes initialization of STX:LIBJAVA's runtime support stuctures as well as initialization of some essential Java objects such as java.lang.Object, java.lang.System, standard I/O streams, system properties etc. Standard JVM does this too before dispatching to any user code.

To initialize STX:LIBJAVA and required Java classes, evaluate following code in Workspace:

  
  Java initialize.
  JavaVM initializeVM.

At this point, Java runtime is initialized and ready to run user code (*). If you look at terminal output Smalltalk/X VM, you may see number of info messages as STX:LIBJAVA loads and initializes Java classes. If the initialization does not proceed, check its configuration (see Section Configuration), especially if the Java release and JAVA_HOME are properly set.

Loading and running HelloWorld

As with standard JVM, STX:LIBJAVA must be told where to look for compiled classes. A class path could be defined in user preferences (see Section Configuration) or ad-hoc using API method #addToClassPath:. The argument is path to directory or .jar file containing the classes - same path you would use with standard JVM. Assuming the compiled HelloWorld class is located under ~/libjava-example-1, evaluate:

  
  Java addToClassPath: '~/libjava-example-1'.

Note, that now the Java is booted and running so we have just changed the classpath in living system.

Now everything is ready to run the code. Evaluate:

  
  JAVA stx libjava examples HelloWorld main: #('World from Java running in ST/X').

The first part -- JAVA org example HelloWorld -- accesses the Java class from Smalltalk. The class is loaded if not already. Sending #main: invokes HelloWorld's public static void main(String[] args). Note, that here you pass a Smalltalk array with Smalltalk string - STX:LIBJAVA interoperability features handle this and automagically convert parameters to the form expected by Java code. For details, refer SectionJava-Smalltalk interoperability

If everything went well, you should see a greeting in system Transcript.

Comments

Reinitializing Java runtime.
If you want (for whatever reason - for example you have modified some STX:LIBJAVA internal classes and Java runtime become completely messed up) - you may always completely flush all Java classes and start over. To do so, evaluate:

  
  Java flushAllJavaResources.

Then, initialize Java runtime as usual.


<info@exept.de> , <stxlibjava-dev@googlegroups.com>

CVS: §Id: N/A §
SVN: $Id: java-getting-started.html,v 1.3 2013-09-06 00:41:42 vrany Exp $