Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to provide correct path to load native library.

Posted on 2009-04-23
21
Medium Priority
?
546 Views
Last Modified: 2013-11-23
Here's what I have
$ ls jniCall/
ArrayManipulation.class  ArrayManipulation.java  JNIMatrices.so  jniCall_ArrayManipulation.h  matrices.cpp

Here's what I do:
$ java -Djava.library.path=./jniCall jniCall.ArrayManipulation
Exception in thread "main" java.lang.UnsatisfiedLinkError: no JNIMatrices in java.library.path
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1709)
        at java.lang.Runtime.loadLibrary0(Runtime.java:823)
        at java.lang.System.loadLibrary(System.java:1030)
        at jniCall.ArrayManipulation.<clinit>(ArrayManipulation.java:11)
Could not find the main class: jniCall.ArrayManipulation.  Program will exit.

What do I need to change for the library to load properly?
0
Comment
Question by:letharion
  • 8
  • 7
  • 6
21 Comments
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 24212855
try renaming the so file to libJNIMatrices.so

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24212861
You should make that 'libmatrices' and do
System.loadLibrary("matrices");

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24212866
(libmatrices.so of course)
0
Independent Software Vendors: 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!

 
LVL 92

Expert Comment

by:objects
ID: 24212887
theres no need to change your code :) just rename the file as I posted above.

0
 
LVL 6

Author Comment

by:letharion
ID: 24212921
Thanks, something certainly works better :)

Instead I now get:
$ java -Djava.library.path=./jniCall jniCall.ArrayManipulation
Exception in thread "main" java.lang.UnsatisfiedLinkError: jniCall.ArrayManipulation.manipulateArray([[ILjava/lang/Boolean;)V
        at jniCall.ArrayManipulation.manipulateArray(Native Method)
        at jniCall.ArrayManipulation.main(ArrayManipulation.java:38)

Ok, so it doesn't find the function now?
$ grep "manipulateArray" *
Binary file ArrayManipulation.class matches
ArrayManipulation.java: public native void manipulateArray(int[][] multiplier, Boolean lock);
ArrayManipulation.java:         am.manipulateArray(ar, am.lock);       //This is line 38 as mentioned above
jniCall_ArrayManipulation.h: * Method:    manipulateArray
jniCall_ArrayManipulation.h:JNIEXPORT void JNICALL Java_jniCall_ArrayManipulation_manipulateArray
Binary file libJNIMatrices.so matches
matrices.cpp:JNIEXPORT void JNICALL Java_ArrayManipulation_manipulateArray(JNIEnv *env, jobject java_obj, jobjectArray elements, jobject lock) {
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24212928
>>theres no need to change your code

There's no need but it would be better to do so to make your library and use of it compliant with standard Unix practices
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24212946
Try recompiling everything
0
 
LVL 6

Author Comment

by:letharion
ID: 24212955
>>>>theres no need to change your code
>>There's no need but it would be better to do so to make your library and use of it compliant with standard >>Unix practices
May I ask what those are? All lowercase? You also removed the "jni" part?

>>Try recompiling everything
I did :/
$ cat Makefile
all:
        g++ -fPIC -fstack-protector -Wall -Wextra -Wformat -Wmissing-include-dirs -Wfloat-equal -Wshadow -Wunsafe-loop-optimizations -Wunreachable-code -Wdeprecated -shared -o libJNIMatrices.so -I/opt/sun-jdk-1.4.2.19/include -I/opt/sun-jdk-1.4.2.19/include/linux matrices.cpp
        javac ArrayManipulation.java
clean:
        rm libJNIMatrices.so
        rm ArrayManipulation.class
 
$ make
g++ -fPIC -fstack-protector -Wall -Wextra -Wformat -Wmissing-include-dirs -Wfloat-equal -Wshadow -Wunsafe-loop-optimizations -Wunreachable-code -Wdeprecated -shared -o libJNIMatrices.so -I/opt/sun-jdk-1.4.2.19/include -I/opt/sun-jdk-1.4.2.19/include/linux matrices.cpp
javac ArrayManipulation.java
 
$ cd ..
$ java -Djava.library.path=./jniCall jniCall.ArrayManipulation
Exception in thread "main" java.lang.UnsatisfiedLinkError: jniCall.ArrayManipulation.manipulateArray([[ILjava/lang/Boolean;)V
        at jniCall.ArrayManipulation.manipulateArray(Native Method)
        at jniCall.ArrayManipulation.main(ArrayManipulation.java:38)

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24212981
>>May I ask what those are? All lowercase?

Yes, with the 'lib' part of the file name identifying it as a shared library, but this gets dropped in use
0
 
LVL 92

Expert Comment

by:objects
ID: 24212989
> Yes, with the 'lib' part of the file name identifying it as a shared library, but this gets dropped in use

which I have already mentioned

0
 
LVL 92

Expert Comment

by:objects
ID: 24212993
> Ok, so it doesn't find the function now?

correct, try regenerating your header

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24213000
Your makefile should probably include javah
0
 
LVL 6

Author Comment

by:letharion
ID: 24213148
Hmm, like this?
$ make clean
rm libjnimatrices.so
rm ArrayManipulation.class
rm jniCall_ArrayManipulation.h
$ make
g++ -fPIC -fstack-protector -Wall -Wextra -Wformat -Wmissing-include-dirs -Wfloat-equal -Wshadow -Wunsafe-loop-optimizations -Wunreachable-code -Wdeprecated -shared -o libjnimatrices.so -I/opt/sun-jdk-1.4.2.19/include -I/opt/sun-jdk-1.4.2.19/include/linux matrices.cpp
javac ArrayManipulation.java
javah -classpath ../ -jni jniCall.ArrayManipulation
$ cd ..
$ java -Djava.library.path=./jniCall jniCall.ArrayManipulation
Exception in thread "main" java.lang.UnsatisfiedLinkError: jniCall.ArrayManipulation.manipulateArray([[ILjava/lang/Boolean;)V
        at jniCall.ArrayManipulation.manipulateArray(Native Method)
        at jniCall.ArrayManipulation.main(ArrayManipulation.java:38)

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 24213174
generate the header before you build the lib

0
 
LVL 92

Expert Comment

by:objects
ID: 24213207
and actually use the definitions generated :)  ie. copy them into you cpp file

0
 
LVL 6

Author Comment

by:letharion
ID: 24213237
Then like this:

wtf?
In case it's of any use, I post the code here.
It's a tar.bz2, not a zip, but I can't post tars.
$ make clean
rm libjnimatrices.so
rm ArrayManipulation.class
rm jniCall_ArrayManipulation.h
$ make
javac ArrayManipulation.java
javah -classpath ../ -jni jniCall.ArrayManipulation
g++ -fPIC -fstack-protector -Wall -Wextra -Wformat -Wmissing-include-dirs -Wfloat-equal -Wshadow -Wunsafe-loop-optimizations -Wunreachable-code -Wdeprecated -shared -o libjnimatrices.so -I/opt/sun-jdk-1.4.2.19/include -I/opt/sun-jdk-1.4.2.19/include/linux matrices.cpp
$ cd ..
$ java -Djava.library.path=./jniCall jniCall.ArrayManipulation
Exception in thread "main" java.lang.UnsatisfiedLinkError: jniCall.ArrayManipulation.manipulateArray([[ILjava/lang/Boolean;)V
        at jniCall.ArrayManipulation.manipulateArray(Native Method)
        at jniCall.ArrayManipulation.main(ArrayManipulation.java:38)

Open in new window

jniCall.zip
0
 
LVL 92

Expert Comment

by:objects
ID: 24213264
your method has the wrong name, should be:

Java_jniCall_ArrayManipulation_manipulateArray

0
 
LVL 6

Author Comment

by:letharion
ID: 24213274
Thanks, it works :)

Ok to split points?
0
 
LVL 92

Expert Comment

by:objects
ID: 24213286
can I ask why (and what comment)? CEHJ's appears to just repeat whatever I posted, or be irrelevant

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24213317
letharion - i don't want any points thanks
0
 
LVL 6

Author Comment

by:letharion
ID: 24213366
It's a deal :)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month20 days, 23 hours left to enroll

810 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