• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 548
  • Last Modified:

How to provide correct path to load native library.

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
letharion
Asked:
letharion
  • 8
  • 7
  • 6
1 Solution
 
objectsCommented:
try renaming the so file to libJNIMatrices.so

0
 
CEHJCommented:
You should make that 'libmatrices' and do
System.loadLibrary("matrices");

Open in new window

0
 
CEHJCommented:
(libmatrices.so of course)
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
objectsCommented:
theres no need to change your code :) just rename the file as I posted above.

0
 
letharionAuthor Commented:
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
 
CEHJCommented:
>>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
 
CEHJCommented:
Try recompiling everything
0
 
letharionAuthor Commented:
>>>>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
 
CEHJCommented:
>>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
 
objectsCommented:
> 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
 
objectsCommented:
> Ok, so it doesn't find the function now?

correct, try regenerating your header

0
 
CEHJCommented:
Your makefile should probably include javah
0
 
letharionAuthor Commented:
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
 
objectsCommented:
generate the header before you build the lib

0
 
objectsCommented:
and actually use the definitions generated :)  ie. copy them into you cpp file

0
 
letharionAuthor Commented:
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
 
objectsCommented:
your method has the wrong name, should be:

Java_jniCall_ArrayManipulation_manipulateArray

0
 
letharionAuthor Commented:
Thanks, it works :)

Ok to split points?
0
 
objectsCommented:
can I ask why (and what comment)? CEHJ's appears to just repeat whatever I posted, or be irrelevant

0
 
CEHJCommented:
letharion - i don't want any points thanks
0
 
letharionAuthor Commented:
It's a deal :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 8
  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now