[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
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
-----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