Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java Native Interface (JNI) - Calling .so from Java in Ubuntu

843829Mar 26 2008 — edited Mar 26 2008
Hello!

I want to call native methods from c++ program by using java

so,
I write Java Program HelloWorld.java
class HelloWorld {
    public native void displayHelloWorld();

    static {
        System.loadLibrary("libhello");
	System.out.println("loaded");
    }
    
    public static void main(String[] args) {
        new HelloWorld().displayHelloWorld();
    }
}
 
Then I Compiled the Java Code javac HelloWorld.java
and
Created .h file using javah HelloWorld

After that I have write C++ code with Native Method Implementation
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>

JNIEXPORT void JNICALL 
Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj) 
{
    printf("Hello world!\n");
    return;
}
I had complied and created libhello.so file by using

gcc -c -I "/usr/lib/jvm/java-6-sun-1.6.0.03/include" -I "/usr/lib/jvm/java-6-sun-1.6.0.03/include/linux" HelloWorldImp.cpp -o libhelloo.so

When I try to run the Program I got error

Exception in thread "main" java.lang.UnsatisfiedLinkError: no libhello in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1682)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1030)
at HelloWorld.<clinit>(HelloWorld.java:5)


I am using Ubuntu and I had tired to set Library path using following commands

1) export PATH="/home/makrand/workspace/cpp/"

2) export PATH="/usr/bin/java"

3) export PATH="/usr/bin/javac"

4) export PATH="/usr/bin/java:/usr/bin/javac:/home/makrand/workspace/cpp/"

5) set LD_LIBRARY_PATH="/home/makrand/workspace/cpp"

6) export LD_LIBRARY_PATH="/home/makrand/workspace/cpp/"


I also tried absulate path for library

But I am still getting same error

Please tell me how to set Library path in Ubuntu ?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 23 2008
Added on Mar 26 2008
1 comment
355 views