[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

JNI Unsatisfied Link Error

Posted on 2005-05-06
11
Medium Priority
?
1,681 Views
Last Modified: 2008-01-09
I'm receiving an Unsatisfied Link Error on a JNI method called as follows:

package myPackage;
public class Class1 {
...
   public native method1();
...
   method1();
...
}

package myPackage;
public class Class2 {
...
   static {
      System.loadLibrary("myDLL");
   }
   public native method2();
...
   method2();
...
}

Both methods are defined in myDLL.dll. The method2() call works, the error occurs when method1() is executed.

What needs to be changed/added to make this work? I figured since the library is loaded up front by the static method, the contents would be available to all classes when they are loaded therafter. Obviously, not.
0
Comment
Question by:softechnics
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13945720
No, you'll have to load the library in Class1 too
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13948178
To extend on CEHJ's comment, I would personally create a third class, like so:

   public class LibraryLoader
   {
      static
      {
         System.loadLibrary( "myDLL" );
      }
   }

Then, just extend this class, in your other classes, like so:

package myPackage;
public class Class1 extends LibraryLoader {
...
   public native method1();
...
   method1();
...
}

package myPackage;
public class Class2 extends LibraryLoader {
...
   public native method2();
...
   method2();
...
}


Likewise, if you wanted to (it's a bit circumstantial to your situation), you could declare the method implementations in the LibraryLoader class also:

     public native method1();
     ...


Hope that helps,
Rob.
0
 
LVL 15

Accepted Solution

by:
aozarov earned 1000 total points
ID: 13950153
I don't think you need to provide System.loadLibrary for every class that uses native calls.
I am using a Main class that loads the library (that main class doesn't have any native calls) which
is being used by other classes.
Are you sure Class2 static initializer is called before you call Class1#method1?
To make sure it does you can add Class2.class.getName(); [or anything else that will trigger loading class2] before you call to Class1#method
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 15

Expert Comment

by:aozarov
ID: 13965153
softechnics,
Did you check if "Class2 static initializer is called before you call Class1#method1"?
0
 
LVL 92

Expert Comment

by:objects
ID: 13965211
InteractiveMind's suggestion would already ensure that.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13965219
I suggested/added that there is no need to call System.loadLibrary( "myDLL" ); in each java file that uses JNI.
0
 

Author Comment

by:softechnics
ID: 13978794
I like the above suggestions, except I cannot extend the DLL Loader class since my main class already extends applet. The static{} loader cannot exist in an interface, so how do I effect multiple extends w/o using interface?
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13979080
softechnics, I don't think you need to apply the loadLibrary in each class. Do you want code to prove you that?
BTW, if you are loading native code in an Applet did you sign it?
0
 

Author Comment

by:softechnics
ID: 13979404
aozarov,
I'm currently working on your suggestion. Our app can be run as a client app or as an applet. If an applet, I don't need the JNI function that's giving me grief. So, my JNI call is only in the main() method. However, main() is static and it appears I cannot call the JNI method from within a static context - or so the compiler tells me.

If I declare LockDisplay static, I get an UnsatisfiedLinkEerror exception at runtime.
If I call the method as ta.LockDisplay, I still get an UnsatisfiedLinkEerror exception at runtime.

My main() method looks something like this:

public class MyClass
    extends Applet
    implements ComponentListener, Runnable, WindowListener
{
...
static {
<Load DLLs here>
}
public native void LockDisplay (String className, String title);
...
    public static void main( String[] args )
    {
        /* Create an instance of the application */
        MyClass ta = new MyClass( );
...
        LockDisplay("MyClass", title);
...
    }
...
}


0
 
LVL 15

Expert Comment

by:aozarov
ID: 13979669
>>  cannot call the JNI method from within a static context - or so the compiler tells me.
You can, if that JNI methods are defined as static.

MyClass ta = new MyClass( );
ta.LockDisplay("MyClass", title);

should work fine if you applied all the procedures correctly (rm *.class, compile, run javah -jni, implment the generated header, compile the native code and created dynamic library which
can be loaded by the static initializer of MyClass
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13979822
If on the other hand, you wanted to go with my suggestion, and extend a class, then you can extend the Applet class, within the ClassLoader, like so:

   public class LibraryLoader extends Applet
   {
      static
      {
         System.loadLibrary( "myDLL" );
      }
   }

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question