Tasks


Using Java Native Interface (JNI) on NetWare

The JAVA\BIN directory includes a file named JNI.ZIP. When you extract this file, it demonstrates simple native method examples for Java 1.x. This section is an example of how to write native methods for the Java 1.4.0 Virtual Machine on NetWare. It is not a comprehensive tutorial on writing native methods.


Tools Required


Description of Files

File Description

MAKEFILE

Make file for NetWare

NWNATIVE.JAVA

Native method java class

NWTEST.JAVA

Test class

README.TXT

 

NWIMPL.C

Native method C implementation

NWMAIN.C

CLIB NLM wrapper - main()


Building a JNI Example

After you unpack the package, complete the following:

  1. Configure the following parameters in Makefile:

    WIN32JAVABASE - Specify where the Win32 JDK is installed.

    NWJAVABASE - Specify where the NetWare JDK is installed (typically the mounted SYS: drive).

    NLMSDKBASE - Specify where the NetWare NDK is installed.

    WATCOMBASE - Specify where Watcom 11.0 is installed.

  2. Enter the following at a Windows command prompt:

    nmake

    The following are the file descriptions:

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

    nmake clean


Installing a JNI Example

After you build the example and map your server volume SYS: to drive G:, complete the following steps 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 the following command:

    copy nwnative.nlm G:\java\bin

  3. Copy the classes to G:\JAVA\CLASSES, and then at the command prompt in Windows enter

    copy *.class g:\java\classes


Running a JNI Example

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

java NWTest


Unloading JNI

To unload, at the server console enter

java -exit


JNI Notes

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

  1. What you can do in your main() function depends on how you build your NetWare Loadable ModuleTM (NLMTM) 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, 0); /* MUST BE CALLED*/

    }

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

    main ()

    {

    ExitThread (TSR_THREAD,);

    }

  2. If you use the Metroworks IDE, the Synchronized linker option can be turned on by clicking the following box:
    ALT-F7 > NLM Linker > Flags > Synchronize
  3. Do not use the standard malloc() or realloc() or free() calls directly. Java provides the following macros in sys_api.h instead:

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

  4. When you link your NLM, you might get the following errors:

    If you find the correct libraries to correct for these warnings, you'll link in a bad prelude.obs and things will not work properly. If you're using C++, see Item 4 below. To prevent these warnings, add the following Makefile option:

    Option NoDefaultLibs

  5. If you are using C++, add the following lines to the link file:

    LIBPath $(WATCOM)\lib386;$(WATCOM)\lib386\netware;

    LIBFile $(WATCOM)\lib386\plbx3s.lib

    Remove the $(PRELUDE) entry from the file directive.

  6. If you used the word stub in the name of any of your native method classes, change sedscript to ensure that it doesn't corrupt your .EXP file.

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.



  Previous Page: Prerequisites  Next Page: Source Level Debugging