繁体   English   中英

JNI HellowWorld unsatisfiedlinkError找不到依赖库

[英]JNI HellowWorld unsatisfiedlinkError Can't find dependent libraries

我已经尝试从以下方法中使用Hello world JNI:

http://home.pacifier.com/~mmead/jni/cs510ajp/

我做了Java课:

public class ACTICamWrapperJNI {
    public native void displayMessage();
  static 
  {
    System.loadLibrary("HelloWorldImp"); 

}

}

并调用它:

     public static void main(String args[]) {

            //</editor-fold>
    System.out.println(System.getProperty("java.library.path"));

   ACTICamWrapperJNI test = new ACTICamWrapperJNI();
   }

cpp文件如下所示:

#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");
}

我已切换到JDK x86并使用X86进行了编译:

C:\Development\testJNI>javac HelloWorld.java

C:\Development\testJNI>javac HelloWorld.java

C:\Development\testJNI>javah -jni HelloWorld

我使用了我在QT文件夹中拥有的MINGW32的gcc(希望可以)

C:\Development\testJNI>gcc -shared -I "C:\Program Files (x86)\Java\jdk1.7.0_40\include" -I"C:\Program Files (x86)\Java\jdk1.7.0_40\include\win32" HelloWorld.cpp -o HelloWorldImp.dll

注意我必须添加共享,否则会出现错误:

 undefined reference to `WinMain@16'

C:\Development\testJNI>"C:\Program Files (x86)\Java\jdk1.7.0_40\bin\java.exe" Main
Exception in thread "main" java.lang.UnsatisfiedLinkError: HelloWorld.displayMessage()V
        at HelloWorld.displayMessage(Native Method)
        at Main.main(Main.java:9)

请注意,我已经确保我正在运行x32 jdk的java并使用x32版本进行编译,因为我的系统上也有x64。

在NetBean IDE上也尝试过并遇到了问题:

Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\Windows\System32\HelloWorldImp.dll: Can't find dependent libraries
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1957)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1882)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1872)
    at java.lang.Runtime.loadLibrary0(Runtime.java:849)
    at java.lang.System.loadLibrary(System.java:1087)
    at au.com.ricor.ACTICamWrapperJNI.<clinit>(ACTICamWrapperJNI.java:15)
    at au.com.ricor.CameraPanel.main(CameraPanel.java:58)

以及我使用的gcc版本是:

C:\Development\testJNI>gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=c:/qt/tools/mingw48_32/bin/../libexec/gcc/i686-w64-mingw32/4
.8.0/lto-wrapper.exe
Target: i686-w64-mingw32
Configured with: ../../../src/gcc-4.8.0/configure --host=i686-w64-mingw32 --buil
d=i686-w64-mingw32 --target=i686-w64-mingw32 --prefix=/mingw32 --with-sysroot=/t
emp/x32-480-posix-dwarf-r2/mingw32 --enable-shared --enable-static --disable-mul
tilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-
threads=posix --enable-libgomp --enable-lto --enable-graphite --enable-checking=
release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --d
isable-sjlj-exceptions --with-dwarf2 --disable-isl-version-check --disable-cloog
-version-check --disable-libstdcxx-pch --disable-libstdcxx-debug --disable-boots
trap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --d
isable-symvers --with-gnu-as --with-gnu-ld --with-arch=i686 --with-tune=generic
--with-host-libstdcxx='-static -lstdc++' --with-libiconv --with-system-zlib --wi
th-gmp=/temp/mingw-prereq/i686-w64-mingw32-static --with-mpfr=/temp/mingw-prereq
/i686-w64-mingw32-static --with-mpc=/temp/mingw-prereq/i686-w64-mingw32-static -
-with-isl=/temp/mingw-prereq/i686-w64-mingw32-static --with-cloog=/temp/mingw-pr
ereq/i686-w64-mingw32-static --enable-cloog-backend=isl --with-pkgversion='rev2,
 Built by MinGW-builds project' --with-bugurl=http://sourceforge.net/projects/mi
ngwbuilds/ CFLAGS='-O2 -pipe -I/temp/x32-480-posix-dwarf-r2/libs/include -I/temp
/mingw-prereq/x32-zlib/include -I/temp/mingw-prereq/i686-w64-mingw32-static/incl
ude' CXXFLAGS='-O2 -pipe -I/temp/x32-480-posix-dwarf-r2/libs/include -I/temp/min
gw-prereq/x32-zlib/include -I/temp/mingw-prereq/i686-w64-mingw32-static/include'
 CPPFLAGS= LDFLAGS='-pipe -L/temp/x32-480-posix-dwarf-r2/libs/lib -L/temp/mingw-
prereq/x32-zlib/lib -L/temp/mingw-prereq/i686-w64-mingw32-static/lib -L/temp/x32
-480-posix-dwarf-r2/mingw32/opt/lib'
Thread model: posix
gcc version 4.8.0 (rev2, Built by MinGW-builds project)

谢谢

威廉

将类从au.com.ricor.ACTICamWrapperJNI重命名为HelloWorld ,或将本机函数(在C ++文件中)从Java_HelloWorld_displayMessage()重命名为Java_au_com_ricor_ACTICamWrapperJNI_displayMessage()

我知道这有一个答案,但是我确定如果您以Debug的形式进行编译,则不会将其重新分发给另一台PC。 您可以尝试为Release进行编译,这应该可以工作。 我只是用它来解决自己的问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM