5.1 Using Java Native Interface (JNI) on NetWare

The java\bin directory includes a file named jni.zip, which contains simple native method examples for Java 1.x. This section provides an example of how to write native methods for NJVM. However, this is not a comprehensive tutorial on writing native methods.

NOTE:  When writing graphical Java applications to run on NetWare, we recommend using the Swing classes from Sun Microsystems.

5.1.1 Required Tools

  • NJVM
  • JDK* 1.4.2 for Win32
  • NetWare SDK March 2003
  • WATCOM* 11.0 (10.6 is not supported.)
  • Microsoft* NMAKE (WATCOM supplies one, but use it at your own risk.)
  • MKS Utilities (grep, cp, rm, sed) in your PATH

These tools and build environment are for the examples in jni.zip. Other compilers (like Metroworks) and build tools can be used.

5.1.2 File Descriptions

The following table provides descriptions of the Java files:

Table 5 File Descriptions

File

Description

makefile

Makefile for NetWare

nwimpl.c

Native method C implementation

nwmain.c

CLIB NLM™ wrapper - main()

nwnative.java

Native method Java class

nwtest.java

Test class

readme.txt

Known issues and recent updates

5.1.3 Building a JNI Example

After you unpack the package, complete the following:

  1. Configure the following parameters in the makefile:

    • WIN32JAVABASE: The location where the Win32 JDK is installed
    • NWJAVABASE: The location where NJVM is installed (typically the mounted sys: drive)
    • NLMSDKBASE: The location where the NetWare NDK is installed
    • WATCOMBASE: The location where Watcom 11.0 is installed
  2. Enter the following at a Windows command prompt:

      nmake
      
  3. To clean the build, enter the following at a Windows command prompt:

      nmake clean
      

5.1.4 Installing a JNI Example

After you build the example and map your server volume sys: (for example, to drive G:), complete the following steps to install JNI:

  1. Check to see if g:\java\classes exists as a directory. If not, enter

      mkdir g:\java\classes
      
  2. Copy the NLM to g:\java\bin by entering

      copy nwnative.nlm g:\java\bin
      
  3. Copy the classes to g:\java\classes, then at the command prompt in Windows, enter

      copy *.class g:\java\classes
      

5.1.5 Running a JNI Example

If your CLASSPATH variable is correct, you can run JNI by entering the following at the command prompt:

  java NWTest
  

5.1.6 Unloading JNI

To unload JNI, enter java -exit at the system console.

5.1.7 JNI Notes

This section contains notes and examples that might be helpful with the JNI process:

  • What you can do in your main() function depends on how you build your NetWare Loadable Module™ (NLM) program. If you include the following option in your makefile, your NLM program can use a synchronized startup:
      Option SYNCHRONIZE
      

    With the synchronize option, you can initialize any global information your NLM program might contain in the main() function as long as you call the following function after initialization has completed:

      void SynchronizeStart();
      

    For example:

      main()
      
      {
      
      /* Do global initialization */
      
      SynchronizeStart(); /*MUST BE CALLED */
      
      ExitThread (TSR_THREAD,0); /* MUST BE CALLED*/
      
      }
      

    If you choose to not use a synchronized startup for your NLM program, you must limit your main() function to the following:

      main ()
      
      {
      
      ExitThread (TSR_THREAD,0);
      
      }
      
  • If you use the Metroworks IDE, the Synchronized linker option can be turned on by pressing Alt+F7, clicking NLM Linker, clicking Flags, then clicking Synchronize.
  • Do not use the standard malloc(), realloc(), or free() calls directly. Java provides the following macros in sys_api.h instead:
    • sysMalloc: Same parameters as malloc()
    • sysFree: Same parameters as free()
    • sysRealloc: Same parameters as realloc()
    • sysCalloc: Same parameters as calloc()

    Using these macros gives you free resource tracking. This also lets the memory used by your NLM program use virtual memory in NetWare 6.5. In some instances, you might prefer memory returned from malloc, such as buffers used for callbacks or ECBs.

  • When you link your NLM, you might get the following errors:
    • Warning! W1008: cannot open math387s.lib: No such file or directory
    • Warning! W1008: cannot open noemu387x.lib: No such file or directory
    • Warning! W1008: cannot open emu387x.lib: No such file or directory
    • Warning! W1008: cannot open clib3s.lib: No such file or directory

    NOTE:  If you attempt to resolve this issue by linking the correct library, the makefile will link the wrong prelude.obj.

    To prevent these warnings, do one of the following makefile options:

    • If you are using C, add the following line to the makefile:
        Option NoDefaultLibs
        
    • If you are using C++, remove the $(PRELUDE) entry from the file directive and add the following lines to the makefile:
        LIBPath $(WATCOM)\lib386;$(WATCOM)\lib386\netware;
        
        LIBFile $(WATCOM)\lib386\plbx3s.lib
        

5.1.8 Using WATCOM 11.0 Compiler Flags for Native Method NLM Programs

Optimized flags are

  /zp=1 /ri /ei /5s /or /ot /w3 /s /zq /ez
  

Debug flags are

  /zp=1 /ri /ei /d2 /od /3s /w1 /s /zq /ez
  

IMPORTANT:  /ri and /ei are critical for building native method NLM programs.