464 Building Java Applications for the iSeries Server with VisualAge for Java 3.5
11.4 Java Native Interface and C
This section discusses using the Java Native Interface and C on the iSeries server.
11.4.1 Setting up JNI and C
No special prerequisite source members are required to use JNI and C. The C and JNI for C
header files, which are actually source members, are included as part of OS/400 and the C
Licensed Program Product. These source members are copied into a C program when using
JNI. The source files are available on the iSeries server in the files shown in Table 11-1 and
Table 11-2.
Table 11-1 Prior to V5R1 source members and their locations on the iSeries server
In V5R1, the source files are in H file in the QSYSINC library.
Table 11-2 V5R1 source members and their locations on the iSeries server
Once the source members are located, follow these steps to use JNI and C:
1. Create a source physical file in your own library to store your own Java program header file
members. For any user-written Java program that is to be linked with C, you must create a
header file and store it in the source physical file. The command to create the header
source physical file is shown in Figure 11-46.
Parameter passing: There are problems with parameters passed by value of types jfloat,
jshort, jchar, and jbyte that are caused by parameter widening that is done by Java and C,
but not by RPG. We recommend that you
do not use float and short. Instead, replace them
with double and int.
Dealing with char and byte is trickier. It is best to avoid them, or pass them from or to Java
as an unsigned int. The problems do not always show up since the translator passes
primitives in registers whenever possible. Using a register for the parameter hides the
problem. Unfortunately, the problem shows up when the call statement becomes complex
enough to use up all of the registers. Therefore, some of the primitive parameters have to
be picked up from the stack.
Source member Location on the iSeries server
jni.h QSYSINC/H/JNI
QJAVA/H/JNI QJAVA/H/JNI
stdio.h QCLE/H/STDIO
stdarg.h QCLE/H/STDARG
jni_md.h QSYSINC/H/JNI_MD
QJAVA/H/JNI_MD QJAVA/H/JNI_MD
Source member Location on the iSeries server
jni.h QSYSINC/H/JNI
stdio.h QSYSINC/H/STDIO
stdarg.h QSYSINC/H/STDARG
jni_md.h QSYSINC/H/JNI_MD
Chapter 11. Java Native Interface (JNI) 465
Figure 11-46 Create Source Physical File (CRTSRCPF)
2. Generate a header file for each Java class that is to use JNI. Make sure that a class file
exists before you create your header file. For example, to create a JNI header file for the
HelloJ program, enter this command:
javah -jni HelloJ
The Java class files and header files are stored in an iSeries integrated file system
directory.
3. Convert the header file from the integrated file system into an iSeries library as a member
of the source physical file H. The previous step shows how to create the H file in your own
library. Figure 11-47 shows the command to copy and convert from the integrated file
system to a library file member.
Figure 11-47 Copy From Stream File (CPYFRMSTMF)
Figure 11-48 on page 466 shows an example of how the header file looks for the HelloJ Java
program.
Create Source Physical File (CRTSRCPF)
Type choices, press Enter.
File . . . . . . . . . . . . . . H Name
Library . . . . . . . . . . . APILIB Name, *CURLIB
Record length . . . . . . . . . 92 Number
Member, if desired . . . . . . . *NONE Name, *NONE, *FILE
Text 'description' . . . . . . . Header File for User JNI Headers
Copy From Stream File (CPYFRMSTMF)
Type choices, press Enter.
From stream file . . . . . . . . > '/home/a999501a/HelloJ.h'
To file member or save file . . > '/qsys.lib/apilib.lib/h.file/HelloJ.mbr'
Member option . . . . . . . . . > *ADD *NONE, *ADD, *REPLACE
Data conversion options . . . . *AUTO *AUTO, *TBL, *NONE
Stream file code page . . . . . *STMF 1-32767, *STMF, *PCASCII
Database file CCSID . . . . . . *FILE 1-65533, *FILE
End of line characters . . . . . *ALL *ALL, *CRLF, *LF, *CR...
Tab character expansion . . . . *YES *YES, *NO

Get Building Java Applications for the iSeries Server with VisualAge for Java 3.5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.