You should only use native methods in cases where pure Java™ cannot
meet your programming needs.
Limit the use of native methods by only using them under these
circumstances:
- To access system functions that are not available using pure Java.
- To implement extremely performance-sensitive methods that can benefit
significantly from a native implementation.
- To interface to existing application program interfaces (API) that allow Java to
call other APIs.
The following instructions apply to using the Java Native
Interface (JNI) with the C language. For information about using JNI with
the RPG language, see the following documentation:
Chapter 11
of the WebSphere® Development
Studio: ILE RPG Programmer's Guide, SC09-2507.
To use the Java Native
Interface (JNI) for native methods, do these steps:
- Design the class by specifying which methods are native methods
with the standard Java language syntax.
- Decide on a library and program name for the service program (*SRVPGM)
that contains native method implementations. When coding the System.loadLibrary()
method call in the static initializer for the class, specify the name of the
service program.
- Use the javac tool to compile the Java source
into a class file.
- Use the javah tool to create the header file (.h).
This header file contains the exact prototypes for creating the native method
implementations. The -d option specifies the directory where you should create
the header file.
- Copy the header file from the integrated file system into a member
in a source file by using the Copy From Stream File (CPYFRMSTMF) command.
You must copy the header file into a source file member for the C compiler
to use it. Use the new stream file support for the Create Bound ILE C/400® Program
(CRTCMOD) command to leave your C source and C header files in the integrated
file system.For more information on the CRTCMOD command and the use of stream
files, see the WebSphere Development
Studio: ILE C/C++ Programmer's Guide, SC09-2712.
- Write the native method code. See Java native
methods and threads considerations for details about the languages
and functions that are used for native methods.
- Include the header file that was created in the previous steps.
- Match the prototypes in the header file exactly.
- Convert strings to American
Standard Code for Information Interchange (ASCII) if the strings are to pass
to the Java virtual machine. For more information, see Java character encodings.
- If your native method must interact with the Java virtual
machine, use the functions that are provided with JNI.
- Compile your C source code, using the CRTCMOD command, into a module
(*MODULE) object.
- Bind one or more module objects into a service program (*SRVPGM)
by using the Create Service Program (CRTSRVPGM) command. The name of this
service program must match the name that you supplied in your Java code
that is in the System.load() or System.loadLibrary() function calls.
- If you used the System.loadLibrary() call in your Java code,
perform one the following task that is appropriate for the J2SDK you are running:
- Include the list of the libraries that you need in the LIBPATH environment
variable. You can change the LIBPATH environment variable in QShell and from
the iSeries™ command
line.
- Or, supply the list in the java.library.path property. You can
change the java.library.path property in QShell and from the iSeries command
line.
Where /QSYS.LIB/MYLIB.LIB is the library that you want
to load using the System.loadLibrary() call, and myclass is the name
of your Java application.
- The path syntax for System.load(String path) can be any of these:
Note: The pathname is typically a string literal enclosed in quotation
marks. For example, you could use the following code:
System.load("/qsys.lib/mylib.lib/myNMsp.srvpgm")
- The libname parameter for System.loadLibrary(String libname) is
typically a string literal in quotation marks that identifies the native method
library. The system uses the current library list and LIBPATH and PASE_LIBPATH
environment variables to search for a service program or i5/OS™ PASE
executable that matches the library name. For example, loadLibrary("myNMsp") results
in a search for a *SRVPGM named MYNMSP or an i5/OS PASE
executable named libmyNMsp.a or libmyMNsp.so.