AIPS++ using JNI in a webservice


This documents my efforts in making a webservice and servlet.

Not just any webservice, but one which:

  • Uses C++ native code via JNI and has lots of libraries to link against
  • Uses Tomcat as a web container
  • Uses Axis for SOAP
  • Uses Ant to build it all

So the end result is a webservice which can run natively compiled code. Then I wrote a simple HttpServlet which calls the webservice.

The main drawback to JNI in Tomcat is that to redeploy a new you must restart Tomcat (ie shutdown and startup). This is because a JVM can only load a library (eg once. That is, it cannot 'write over' an existing library.

Email me with questions - this is just an outline of the process, not a step-by-step tutorial.

How do we do that?

Create Native Library and test JNI on the command-line

  • Choose the C++ functions to expose
  • Write public native methodName(args) in a Java class for each of them
  • This will eventually become the Web service interface.
  • Compile Java code.
  • Use javah on the .class file to produce C++ header files
  • Ensure the C++ header files match you function definitions in your .cpp files. An example protoype is:
    • JNIEXPORT jstring JNICALL Java_org_ausvo_quanta_Quanta_convert (JNIEnv *, jobject, jstring, jstring);
  • For more JNI info, See
  • In a static function of Java, load the library:

static { System.loadLibrary("XXXX") ; // nb. not } QuantaServiceLocator locator = new QuantaServiceLocator() ;

defaultWebSerivce = locator.getquanta(); // Note you can call this will a URL object if you don't want the default location

String quantityAndUnit = args[0];

String targetUnit = args[1];

System.out.println("Quanta.convert() = " + defaultWebSerivce.convert(quantityAndUnit,targetUnit));