NonStop Server for Java (NSJ) Programmer's Guide (NSJ 2.0+)
Sockets
Use only the NonStop
TM
Server for Java 2.0 socket implementation in java.net (the standard Java socket). All socket I/O
calls are thread-safe and nonblocking.
If you use a socket in a native call, you do not get the benefit of multithreading functionality. The socket call blocks the
whole JVM. If you use Java sockets, the JVM can schedule other threads while one thread is waiting for socket completion.
Calling Java Methods From C or C++
You can create your own C or C++ program and use the Invocation API to load the Java Virtual Machine (JVM) into an
arbitrary native program. Code written in C++ must be compiled using the -Wversion compiler command-line option. In
the Compaq environment, you must link the JVM archives into the custom-built main program.
The remainder of this subsection shows the source programs for the Invocation API demo and tells how to run it.
Source for the Invocation API Demo
The source code for the Java class that writes an array of Strings to the screen is:
//---------------------------------------------------------------------
// Class: Hello
// Usage: java Hello
//
// Print array of Strings passed to it to standard out.
//---------------------------------------------------------------------
public class Hello {
public static void main(String args[]) {
System.out.println("The main method of Hello.class was passed:");
for (int i=0; i < args.length; i++) {
System.out.print(args[i] + " ");
}
System.out.println("");
}
}
The following C main source program, main.c, uses the Invocation API.
#include
static jobjectArray
CreateStringArray(JNIEnv *env, char **strv, int strc)
{
jarray class;
jarray array;
jstring string;
int i;
class = (*env)->FindClass(env, "java/lang/String");
if (class == 0) return 0;
array = (*env)->NewObjectArray(env, strc, class, 0);
if (array == 0) return 0;
for (i = 0; i < strc; i++) {
string = (*env)->NewStringUTF(env, *strv++);
if (string == NULL) return 0;
(*env)->SetObjectArrayElement(env, array, i, string);
(*env)->DeleteLocalRef(env, string);
}
return array;