我有一个简单的JNI项目,它编译。但是当我运行它时,我收到以下错误:
$ java HelloJNI
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.ClassFormatError: Extra bytes at the end of class file HelloJNI
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
我不得不改变很多东西。在Ubuntu上使用gcc;它有效:
ghostrider@:~/programing/java/jni/HelloJNI$ java -Djava.library.path=. HelloWorld
Hello from C!
ghostrider@:~/programing/java/jni/HelloJNI$ ls
ctest.c HelloWorld.class HelloWorld.h HelloWorld.java libctest.so
我使用此link来使其正常工作。
所以我现在的问题是我可以使用gcc编译Windows体系结构,还是必须安装MinGw才能这样做? 感谢大家的帮助。
我刚刚验证了这适用于MinGw-64
C:\Users\sansari\Downloads\x86_64-w64-mingw32-gcc-4.8.0-win64_rubenvb\mingw64\java>gcc -o ctest.dll -shared -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" ctest.c -m64
C:\Users\sansari\Downloads\x86_64-w64-mingw32-gcc-4.8.0-win64_rubenvb\mingw64\java>ls
HelloWorld.class HelloWorld.h HelloWorld.java ctest.c ctest.dll
C:\Users\sansari\Downloads\x86_64-w64-mingw32-gcc-4.8.0-win64_rubenvb\mingw64\java>java HelloWorld
Hello from C!
答案 0 :(得分:0)
我相信你做错了,因为类加载器抱怨课程被破坏了。
一般情况下需要执行以下步骤(您还没有告诉我们您做了哪一个)
javac HelloJNI.java
javah -jni HelloJNI
# compile your HelloJNI.cpp
将库名修改为Java类
instead of : System.loadLibrary("hello");
it should be: System.loadLibrary("HelloJNI");
以下步骤在CentOS 7上生成一个有效的DLL(抱歉没有MinGW for Windows)
javac HelloJNI.java
javah HelloJNI
x86_64-w64-mingw32-gcc -Wl,-kill-at -Wall -O0 -o HelloJNI.dll -shared \
-I. -I/usr/lib/jvm/java-openjdk/include \
-I/usr/lib/jvm/java-openjdk /include/linux HelloJNI.cpp
将HelloJNI.class
和HelloJNI.dll
复制到64位Windows计算机上的同一目录中。
java HelloJNI
输出
Hello World!