Calling C or C++ routines from Java

To call C or C++ routines from Java, conform to the following steps:

  1. Compile the Java code.

  2. Use javac -h to generate header files— The function declarations listed in the generated header file are those that must be exported by the user-JNI DLL.

    To export functions, either specify export$ in the function definition or use the linker option -export_all.

  3. Compile the C or C++ code using c89 compiler. C++ code must be compiled using the compiler command-line options as explained in Linker and compiler options.

    NOTE:

    JVM does not support JNI application compiled using c11 compiler.

    For example:
    /usr/bin/c89 -g -I /usr/tandem/nssjava/jdk110_l11/include -I /usr/tandem/nssjava/jdk110_l11/include -I. -I /G/system/system -Wallow_cplusplus_comments -Wlp64 -Wextensions -D_XOPEN_SOURCE_EXTENDED=1 -Wnowarn=141,209 -Wcall_shared -Wsystype=oss -Wtarget=tns/x -c SystemInfo.c

    If the native code has large variables on the stack, then calling this native code might exceed the default stack space provided for each thread. If the native code exceeds the amount of stack space allocated for it, then it results SIGSTK.

    To prevent overflowing the available stack space, consider allocating large variables on the heap rather than using the stack. Otherwise, you can increase the default stack size for each thread by specifying the -Xss option when starting java. This option increases the stack size for every thread.

    For more information about the -Xss option, see java in the NSJ 11Tools Reference Pages.

  4. Create a DLL file (.so file type) and specify the linker option as explained in Linker and compiler options.

    After specifying the linker option, set the _RLD_LIB_PATH environment variable to point to where the created DLL file resides by using the following command:

    export _RLD_LIB_PATH=dll-path

    where, dll-path is the directory where the user DLL resides.

    For example:
    /usr/bin/xld TandemWrap.o SystemInfo.o -o libCompanySysInfo.so -set SYSTYPE OSS -set HIGHPIN ON -set data_model lp64 -set CPlusPlusDialect neutral -dll -lcre -lcrtl -export_all

    For more information, see _RLD_LIB_PATH.

The nsjjni demo shows an example of how to create a library file, see Table 3: Demonstration programs available in NSJ 11 to view the directory location of the demo programs. The demo also shows the conversion between the TNS and IEEE floating-point.