Difference between revisions of "SimpleITK/GettingStarted/A visual guide to SimpleITK in Java"

From KitwarePublic
Jump to navigationJump to search
(Created page with "== Java == Java and SimpleITK are a natural fit. Like the other wrapped languages, SimpleITK's Java bindings have a language-specific component (traditional Jar file), and a n...")
 
Line 13: Line 13:
 
=== GaussianExample: ===
 
=== GaussianExample: ===
  
Our first class <code>Code/GaussianExample.java<code>
+
Our first class <code>Code/GaussianExample.java</code>
  
<code>
+
<pre>
 
package org.itk.simple.example;
 
package org.itk.simple.example;
  
Line 42: Line 42:
  
 
}
 
}
<code>
+
</pre>
  
 
If Eclipse is doing it's job, you should see some errors on lines 19-21.  This is because we haven't told Eclipse where to find SimpleITK's jar file.
 
If Eclipse is doing it's job, you should see some errors on lines 19-21.  This is because we haven't told Eclipse where to find SimpleITK's jar file.

Revision as of 10:39, 9 February 2012

Java

Java and SimpleITK are a natural fit. Like the other wrapped languages, SimpleITK's Java bindings have a language-specific component (traditional Jar file), and a native component (native shared library). This combination requires a little more setup, but is mainly transparent to the developer.

Eclipse setup

Download Eclipse and install on the platform of your choice. Eclipse is a commonly used IDE for Java, and makes development, debugging and deployment particularly streamlined.

The first step is to create a new project in Eclipse. Simply choose File --> New --> Project..., choosing Java Project in the project wizard dialog, and naming the project as you like. In this example, our project is called SimpleITK Demo. Create a new class by choosing File --> New --> Class, or simply copy the code below and paste into the project item in the Package Explorer view and Eclipse will automatically create the class and hierarchy for you.

GaussianExample:

Our first class Code/GaussianExample.java

package org.itk.simple.example;

import org.itk.simple.Image;
import org.itk.simple.SimpleITK;

public class GaussianExample {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		if (args.length < 2) {
			System.err.println("Usage: 'Gaussian <input> <output>'");
			System.exit(1);
		}
		System.out.println("Starting to blur " + args[0]);
		// Grab a file
		Image image = SimpleITK.readImage(args[0]);
		Image output = SimpleITK.discreteGaussian(image);
		SimpleITK.writeImage(output, args[1]);
		System.out.println("Finished blurring, writing to " + args[1]);

	}

}

If Eclipse is doing it's job, you should see some errors on lines 19-21. This is because we haven't told Eclipse where to find SimpleITK's jar file.

Adding SimpleITK to the Build Path

Right click on the project in the Package Explorer view and choose Build Path --> Configure Build Path....

.. figure:: Images/ChangeBuildPath.dropshadow.png

  :align: center

Configure the Build Path for our Eclipse project

In the Properties dialog, click on Add External JARs... and navigate to the SimpleITK jar file. When selected, click the down arrow to expose the options for the jar.

.. figure:: Images/BuildPathSettings.dropshadow.png

  :align: center

.. centered:: SimpleITK jar configuration

The three options of interest are: Source attachment, Javdoc location and Native library location. The Source attachment specifies where the source code for the SimpleITK jar file resides. In our case, it is distributed as simpleitk-source.x.x.jar where x.x.x is the version number of SimpleITK. The source attachment is usefule for debugging the SimpleITK library, if necessary, because it allows the debugger to step through classes provided in the SimpleITK jar file. This setting is optional.

The Javadoc location is also optional, but exceedingly helpful in developing with Java. Having Javadoc available provides Eclipse with in-line documentation for each function, if provided.

The last option, Native library location is required. Because SimpleITK is a C++ library, all functionality is provided through the ::abbr:JNI (Java Native Interface) specification. When the SimpleITK classes are loaded, a static block loads the native library to provide all the functionality to Java. This option tells Eclipse where to search for the library; without it a UnsatisfiedLinkError is thrown::

 Exception in thread "main" java.lang.UnsatisfiedLinkError: no SimpleITKJava in java.library.path

at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1758) at java.lang.Runtime.loadLibrary0(Runtime.java:823) at java.lang.System.loadLibrary(System.java:1045) at org.itk.simple.SimpleITKJNI.<clinit>(SimpleITKJNI.java:62) at org.itk.simple.SimpleITK.readImage(SimpleITK.java:33) at org.itk.simple.example.GaussianExample.main(GaussianExample.java:19)

Set the Native library location to the directory containing the platform specific JNI library, i.e. libSimpleITKJava.jnilib on Mac OSX, libSimpleITKJava.so on Linux and SimpleITKJava.dll on Windows. After this our example code runs correctly. If running from the command line, the location needs to be specified to the JVM, e.g. -Djava.library.path=/path/to/SimpleITKRuntime.

SimpleITK Java Conventions ..........................

The SimpleITK Java bindings closely follow the C++ conventions, i.e. each class contains the public member functions. However, the functional interface is handled differently in Java. In particular, every static Java function must belong to a class, unlike C++. In SimpleITK, the functional interface is contained in a class called org.itk.simple.SimpleITK. This class contains the functional interfaces as static member functions, i.e. org.itk.simple.SimpleITK.readImage as shown :ref:in the example <GaussianExample>. The naming conventions for all SimpleITK classes follows the C++ conventions, but member functions and the function interface follow the Java conventions of using CamelCase with the first letter lowercase. In Java, the C++ function ::itk::simple::ReadImage becomes org.itk.simple.SimpleITK.readImage.