简体   繁体   中英

UnsatisfiedLInkError Eclipse JNI (works from command line but not in Eclipse) package names

I'm able to run a program from the commmand line by typing "java main" where main.java and main.class are in the same directory as well as any related classes. This runs fine. When I try to run the same program in Eclipse I get Unsatisfied link errors. I think this is related to the JVM being used. I think that the command line java call is using a different JVM then eclipse. How can you specify which JVM java uses on the command line?

I'm getting an UnsatisfiedLinkError when I run a program in Eclipse that uses native libraries.

This isn't a typical "cannot find...." link error I believe it has actually found the file but there is some other problem.

Exception in thread "main" java.lang.UnsatisfiedLinkError: com.me.this.MyClass.MyMethod(Ljava/lang/String;)I

You can see that if it just could not find the shared library it would say something like:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no MySharedFile.so in java.library.path

So I believe it is finding the file.

Some other issues that are relavent are the fact that if i run the program from the command line instead of in eclipse it finds the .so and runs the program perfectly. Also I had this program running before in a different Eclipse that was using an older 1.6 JVM. I've tried to use that in this eclipse but it hasn't helped.

Is this a problem finding the .so shared file? Or something completely different like I'm using the wrong JVM. I used strace on the java command line program and it appears it's using the new 1.7jdk the same one I'm using now in Eclipse and it will not work.

The .so is in /usr/lib64 and I've also created a -Djava.library.path=... entry in the vm arguments for the run configuration just in case.

I added these try catch around the load:

static
    {
        try{
            System.loadLibrary("MyAwesomeLibrary");
            System.out.println("MyAwesomeLibrary library loaded \n");
        }
        catch(UnsatisfiedLinkError e){
            System.out.println("Did not load library");
            e.printStackTrace();
        }

    }

And I get:

MyAwesomeLibrary library loaded 

Exception in thread "main" java.lang.UnsatisfiedLinkError: com.me.this.MyClass.MyMethod(Ljava/lang/String;)I
    at com.me.this.MyClass.MyMethod(Native Method)
    at com.me.this.Main.main(Main.java:8)

It's being called froma main class that looks like this:

public class Main
{
        public static void main( String[] args )
        {
                ClassThatContainsLoadedLIbrary x = new ClassThatContainsLoadedLibrary();
                int y = x.Ping( "thisaddress" );

So it appears that it's loading...at least it's getting to the print statement without link errors. the UnsatisfiedLink errors when it actually triest to use the library.

I've been working on this problem for weeks so would really appreciate it if some one had some insight into this. Thanks.

Look at this: http://docs.oracle.com/javase/7/docs/api/java/lang/UnsatisfiedLinkError.html .

Thrown if the Java Virtual Machine cannot find an appropriate native-language definition of a method declared native .

The problem is not with the JVM per se, but with the JVM being unable to find your native libraries. You need to specify the path where the native libraries are stored. This can be done by adding the following as an argument to your JVM in eclipse:

-Djava.library.path=...

Here, take a look at this: http://mindprod.com/jgloss/runerrormessages.html#UNSATISFIEDLINKERROR

  • If you get the error after the class containing the native method is safely loaded, when you invoke a native method, make sure you generated your *.h file with the fully qualified and not simply 生成了*.h文件 ,而不仅仅是
  • If you get the error after the class containing the native method is safely loaded, when you invoke a native method, check that the *.cpp method signatures exactly match those in the *.h file. You should see method names like this: that start with the word Java and contain the package, class and method name all strung together. 以Java单词开头,包含所有的包,类和方法名称。 Make sure you remembered to implement all the methods.
  • You need to regenerate the *.h and recompile the *.c file if you change the package name.

  • You should probably go through this list to make sure you're doing all the things correctly.

    您应该在控制台中使用java查找Java选项。

    java -version:1.6 MyClass

    Right click your project ----> Java Build Path ---> Libraries tab.

    Then select JRE System library , and click Edit to bring the following screen to configure 在此处输入图片说明

    The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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