关于路径找不到的问题。
文件在/home/xxxxx/workspace/JNI_Test/org/dvb/test/NativeStuff.class
org.dvb.test.NativeStuff package名字
In order to be sure, use -classpath option:
javah -classpath /home/xxxxx/workspace/JNI_Test org.dvb.test.NativeStuff
注意输类的名字 org.dvb.test.NativeStuff
(And make sure the /home/xxxxx/workspace/JNI_Test/org/dvb/test/NativeStuff.class file exists)
http://home.pacifier.com/~mmead/jni/cs510ajp/
One of the best ways to learn a new programming skill is by example. Inkeeping with tradition, we present the canonical programming example thatsimply prints the words "Hello World!" to the display. The twist is thatJava code will invoke a native function to do the actual printing via the Clibrary function, printf. A more detailed explanation of this examplecan be foundhere.
1. Create the Java files. First, create the two Java files as shown below.
HelloWorld.java2. Compile the Java files.class HelloWorld { public native void displayMessage(); static { System.loadLibrary("HelloWorldImp"); } }Main.javaclass Main { public static void main(String[] args) { HelloWorld hello = new HelloWorld(); hello.displayMessage(); } }
3. Create the header file. Uses the .class file created previouslyto create HelloWorld.h.(Note the -jni argument to javah.)javac HelloWorld.java javac Main.java
4. Create the C++ file. This example shows the native code with a .cppextension, but this file could just have easily had a .c extension since thereis nothing "C++" about it.javah -jni HelloWorld
HelloWorld.cpp5. Build the shared library from native code. This is where some of the differences show up. Make sure that the search paths are correct and thatthe file extensions are what the compiler expects. Also, the convention inUnix is to prepend lib onto the names of library files and to append .so onto the names of shared library files.#include <stdio.h> #include "HelloWorld.h" // this header file was generated by javah JNIEXPORT void JNICALL Java_HelloWorld_displayMessage(JNIEnv *env, jobject obj) { printf("Hello World!\n"); }
6. Execute the program. On Unix, you may have to move the shared libraryinto a directory that's in your search path, or add the current directory toyour path. Under Windows NT, the current directory is searched by default.# This works on sirius.cs.pdx.edu, note .C extension g++ -G -I/pkgs/jdk1.1.1/include -I/pkgs/jdk1.1.1/include/solaris HelloWorld.C -o libHelloWorldImp.so : This should work under NT. Replace the include paths to match your environment : and be sure to include Sun's JDK not Microsoft's! cl -Im:\jdk1.1.5\include -Im:\jdk1.1.5\include\win32 -LD HelloWorld.cpp -FeHelloWorldImp.dll
java Main Hello World! <--- this is displayed on the screen