Calling Java methods from C or C++

You can create your own C or C++ program. Follow these guidelines to use the Invocation API to load the JVM in an arbitrary native program:

  • NSJ 11 supports only the POSIX User Thread library. As a result, the following issues must be considered when migrating to NSJ 11:

    • Java application which uses the Invocation API must necessarily link with the POSIX User Thread library. An application must link with WPUTDLL. Set the _RLD_LIB_PATH environment variable to the location of libjvm.so as shown:

      $ export _RLD_LIB_PATH=$<java_home_path>/lib/server
  • A native application can use the JNI_CreateJavaVM() call to become a Java process, and such applications are called JNI applications. The native application can be either multi-threaded or single threaded. If an application calls the JNI_CreateJavaVM() on the main thread, then the main thread becomes the Java thread after creating the virtual machine. If an application already contains a VM, another thread can become a Java thread by invoking AttachCurrentThread(). In either case, if the calling thread is the main thread and if distributed GC or compiler process is enabled, the main thread stack must be shared by javahelper processes. As the main thread stack cannot be shared across javahelper processes, the distributed GC cannot be enabled when the main thread is the Java thread. Hence, JNI_CreateJavaVM() and AttachCurrentThread() return JNI_EUNSUPPORTED error if they are called from the main thread with distributed GC or compiler process enabled.

    NOTE:

    All the instances of distributed GC in this manual refer to non-serial GC. For more information, see Garbage collection (GC).

  • Compile code written in C++ by using the compiler command-line options as explained in Linker and compiler options.

  • NSJ 11 provides DLLs. Therefore, you can build your own executable and link it to the JVM DLL, libjvm.so.

    For more information, see invocation_api demo provided with the NSJ 11 installation.

  • JNI applications cannot enable parallel and CMS GC if the JVM is loaded in the main thread.

  • Do not set signal handlers for the following signals:

    • SIGSEGV
    • SIGPIPE
    • SIGCHLD
    • SIGINT
    • SIGQUIT
    • SIGTERM
    • SIGHUP
    • SIGWINCH
    • SIGALRM
    • SIGSTK
    • SIGILL
    • SIGTIMEOUT
  • Set the executable to use IEEE floating-point.

NSJ 11 does not support the signal-chaining facility implemented in some other vendors' JVMs.

When a program uses the Invocation API to start a JVM, its function returns parameters of type float or double that are in IEEE floating-point format. Any parameters of type float or double that are passed to NSJ 11 must be in IEEE floating-point format. If such a program requires conversion between TNS floating-point format and IEEE floating-point format, the Guardian Procedure Calls Reference Manual documents a series of procedures with names beginning with NSK_FLOAT_ that can be used to convert float and double data between the two formats.

To run the Invocation API demo, follow the instructions in the README file in directory /usr/tandem/nssjava/jdk110_l11/demo/invocation_api. Directory location of invocation_api demo programs are listed in Table 3: Demonstration programs available in NSJ 11.