[opensuse-programming] Problem using Java Native Interface
Hi, I am trying to build a simple program using Java Native Interface, but trying to execute the Java program gives me an error. I am giving the program and the procedure that I have used here. Code: public class MyClass { public static native void hello(); public static void main (String args[]) { MyClass m=new MyClass(); m.hello(); } static { System.loadLibrary("MyClass"); } } Then, I have compiled this program file using the command javac MyClass.java. I have created another C program file named Hello.c, as given below: Code: #include "MyClass.h" #include <stdio.h> #include <jni.h> JNIEXPORT void JNICALL Java_MyClass_hello (JNIEnv *env, jclass jc) { printf("Hello World"); } Then, I have compiled this C program using the command: gcc Hello.c -fPIC -I /usr/lib64/jvm/java-1.6.0-openjdk-1.6.0/include/linux/ -I /usr/lib64/jvm/java-1.6.0-openjdk-1.6.0/include/ -shared -o libMyClass.so This compiled successfully without giving any errors. Now, I tried to run the program, using the command: java -cp . MyClass Exception in thread "main" java.lang.UnsatisfiedLinkError: no MyClass in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1681) at java.lang.Runtime.loadLibrary0(Runtime.java:840) at java.lang.System.loadLibrary(System.java:1047) at MyClass.<clinit>(MyClass.java:34) Could not find the main class: MyClass. Program will exit. As for the version of the compiler, and the Virtual Machine, java -version gives me the following information: java version "1.6.0_18" OpenJDK Runtime Environment (IcedTea6 1.8.1) (suse-1.1.5-x86_64) OpenJDK 64-Bit Server VM (build 14.0-b16, mixed mode) I would be grateful if someone on this list could help me on this. Thanks for reading this! -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
On Sat, 2010-10-16 at 20:07 +0530, Arani Bhattacharya wrote:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no MyClass in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1681) at java.lang.Runtime.loadLibrary0(Runtime.java:840) at java.lang.System.loadLibrary(System.java:1047) at MyClass.<clinit>(MyClass.java:34) Could not find the main class: MyClass. Program will exit.
One possibility is that Java can’t find the shared library. Usually you need to set LD_LIBRARY_PATH to tell Java where the library is. Here is a shell script I use. #! /bin/sh export LD_LIBRARY_PATH=/usr/local/lib java -jar /usr/local/share/dea.jar Here the shared library is in /usr/local/lib. But the shell script could be changed to put it anywhere. I also put all the Java .class files in a jar file, dea.jar. -- JDL -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Arani Bhattacharya wrote:
Hi,
I am trying to build a simple program using Java Native Interface, but trying to execute the Java program gives me an error. I am giving the program and the procedure that I have used here.
Code:
public class MyClass {
public static native void hello();
public static void main (String args[]) { MyClass m=new MyClass(); m.hello();
} static { System.loadLibrary("MyClass"); } }
Then, I have compiled this program file using the command javac MyClass.java.
Hmm... For JNI check the some of the relevant Java Docs http://download.oracle.com/javase/1.4.2/docs/guide/awt/1.3/AWT_Native_Interf... http://download.oracle.com/javase/1.5.0/docs/guide/jni/spec/jniTOC.html http://java.sun.com/docs/books/jni/download/jni.pdf the above are a start..In particular look at the getting started section in the last link..... - -- ============================================================================== I have always wished that my computer would be as easy to use as my telephone. My wish has come true. I no longer know how to use my telephone. Bjarne Stroustrup ============================================================================== -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org/ iEYEARECAAYFAky6xV8ACgkQasN0sSnLmgIVFACg3VNecH8h1XbsEvfrNbsbLfdP tg4AoI9NwGnZQQ9Y0AJ8RYBSCTZOt/PS =1l4i -----END PGP SIGNATURE----- -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
On Sun, Oct 17, 2010 at 3:14 PM, G T Smith <grahamsmith@gandalfsemporium.homelinux.com> wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Arani Bhattacharya wrote:
Hi,
I am trying to build a simple program using Java Native Interface, but trying to execute the Java program gives me an error. I am giving the program and the procedure that I have used here.
Code:
public class MyClass {
public static native void hello();
public static void main (String args[]) { MyClass m=new MyClass(); m.hello();
} static { System.loadLibrary("MyClass"); } }
Then, I have compiled this program file using the command javac MyClass.java.
Hmm...
For JNI check the some of the relevant Java Docs
http://download.oracle.com/javase/1.4.2/docs/guide/awt/1.3/AWT_Native_Interf... http://download.oracle.com/javase/1.5.0/docs/guide/jni/spec/jniTOC.html http://java.sun.com/docs/books/jni/download/jni.pdf
the above are a start..In particular look at the getting started section in the last link.....
- -- ============================================================================== I have always wished that my computer would be as easy to use as my telephone. My wish has come true. I no longer know how to use my telephone.
Bjarne Stroustrup ============================================================================== -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org/
iEYEARECAAYFAky6xV8ACgkQasN0sSnLmgIVFACg3VNecH8h1XbsEvfrNbsbLfdP tg4AoI9NwGnZQQ9Y0AJ8RYBSCTZOt/PS =1l4i -----END PGP SIGNATURE----- -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
Thanks a lot for your ideas. I checked the references and discovered that merely adding the current directory to the classpath does not force Java to check for shared libraries (required for native access). Instead I had to add -Djava.library.path=$PWD for this task. Thanks once again. -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
participants (3)
-
Arani Bhattacharya
-
G T Smith
-
John D Lamb