JNI Basics

From Weiss's C++ for Java Programmers Initial Code: To run the code
  1. Compile the Java Code
         javac HelloNativeTest.java
    
    This creates two .class files: HelloNativeTest.class, HelloNative.class

  2. The HelloNative.class file is used to create the proper .h file for the C++ code by using the command of:
         javah HelloNative
    
  3. Now to compile the C++ code (shown using UNIX commands):
        # g++ -c -fPIC -I$JAVA_HOME/include -I$JAVA_HOME/include/linux HelloNative.cpp
        g++ -c -fPIC HelloNative.cpp
    
    
    This create the HelloNative.o file.

  4. Now create the shared library object file with the command:
        g++ -shared -o libHelloNative.so HelloNative.o
    
  5. Before running the program the java code has to know where the shared library file is located so it can be loaded, so we need to set the LD_LIBRARY_PATH environment variable to its locations. The command below assumes the file is in the current directory.
         setenv LD_LIBRARY_PATH .
    
  6. Now to run the java program with
         java HelloNativeTest
    

Other Examples from Weiss