This content has been marked as final. Show 8 replies
Thanks a lot for the help. I replaced -G flag with -shared flag but I kept the -I flag as it is. Now I am getting runtime error (details below), would you please help to resolve it.
I have got the following error/result of the command this time:
g++ -shared -I/opt/jdk1.6.0_24/include -I/opt/jdk1.6.0_24/include/linux HelloWorld.c -o libHelloWorld.so
HelloWorld.c:10:2: warning: no newline at end of file
Although the .so file was created successfully this time but when I ran the program with ./java HelloWorld command, I have got a runtime error on it:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no HelloWorld in java.library.path
Could not find the main class: HelloWorld. Program will exit.
Thanks a lot for the help,
You're not finding your native library. You need HelloWorld.dll or libHelloWorld.so to be in the current directory, or the PATH, or the java.library.path, when you execute that command. I don't see how that can be the case if ./java works, as java is installed in a 'bin' directory in the JRE or JDK. Unless you are polluting the JRE/JDK with your own source and object code, which you shouldn't be doing either.
I am not sure about the java.library.path thing, should I get help to fix that?
Otherwise, I have put all the files related to this program (HelloWorld.c, HelloWorld.class, HelloWorld.h, HelloWorld.java and libHelloWorld.so) in the bin folder of JDK (/opt/jdk1.6.0_24/bin).
Should I put my code/files elsewhere and how will that effect the path requirement?
I tried the command that is given in post above. I actually tried the path with file name (java -Djava.library.path=/opt/jtestcode/libHelloWorld.so HelloWorld) and without file name (java -Djava.library.path=/opt/jtestcode/ HelloWorld). Both these times, I got the same error:
java -Djava.library.path=/opt/jtestcode/libHelloWorld.so HelloWorld
Exception in thread "main" java.lang.ClassFormatError: HelloWorld (unrecognized class file version)
Would you please help resolve this error.
Thanks a lot for all the help.
Well, you've specified the NAME of your .so file, not the path to it: don't put the filename in the java.library.path
I would recommend commenting out all references to the JNI code, recompile and see if
works first. ONce you've got that, just uncomment the cladd to loadLibrary() and make sure that compiles and runs
then uncomment calls into the JNI and see if that works