javac JniTest.java -d .
javah -classpath . -o JniTest.h com.apple.cai.JniTest
g++ -dynamiclib -o JniTest.jnilib JniTest.cpp -framework JavaVM -I/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home/include/darwin
java -Djava.library.path=/Users/apple/JavaProjects -classpath . com.apple.cai.JniTest
使用以上方式运行就是一直报异常,一直找不到原因
Exception in thread "main" java.lang.UnsatisfiedLinkError: no JniTest in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1886)
at java.lang.Runtime.loadLibrary0(Runtime.java:849)
at java.lang.System.loadLibrary(System.java:1088)
at JniTest.(JniTest.java:7)
javac HelloWorld.java
javah -jni HelloWorld
g++ -I/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home/include/darwin -c HelloWorld.cpp
g++ -dynamiclib -o libhelloworld.jnilib HelloWorld.o
java HelloWorld
区别就在于第三第四个命令行(第三行的路径是我电脑上的jni.h的路径地址),而且使用后一种方式竟然不用指定搜索路径(疑惑),如果有大神知道什么原因,望指点一二。不甚感激