Sign in to follow this  
OpenGL_Guru

[java] Java(JNI) to C++, code inside

Recommended Posts

hi all. i am currently working on a project concerning web services. the idea is to get the application side in C++, for a variety of reasons, including speed and platform independent and have java have the job of web services, again for portability concerns.(this should run on windows, linux, mac, etc). to do this you need JNI - that is create a "bridge" between the java bytecode and the C++ functions that you need to give information to and receive information from. i am using g++ on linux(ubuntu distr.). before i get started i have to make sure my env variables are set: export JAVA_HOME=/home/usr/JDK6_4_dev/jdk1.6.0_04 PATH=$PATH:/home/usr/JDK6_4_dev/jdk1.6.0_04/bin export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${JAVA_HOME}/lib **Note - for some reason putting this in my .bash_profile does not save/work, even after i exit. everytime i log on i have to keep doing this. why?? #1. i have my .java file called HelloWorld.java -----------------------------------------------
class HelloWorld
{
 public native void displayMessage();
 static
 {
  System.loadLibrary("HelloWorldImp");
 }
        public static void main(String[] args)
        {
                HelloWorld hello = new HelloWorld();
                hello.displayMessage();
        }

}


----------------------------------------------- 2. $javac HelloWorld -- creates HelloWorld.class 3. $javac -jni HelloWorld -- creates the C headerfile "wrapper" to go along with my .cpp file 4. create the .cpp file., HelloWorld.cpp ---------------------------------
#include <iostream>
#include "HelloWorld.h"

using namespace std;

JNIEXPORT void JNICALL Java_HelloWorld_displayMessage(JNIEnv *env, jobject obj)
{
cout << "hello world!" << endl;
}


---------------------------------------- 5. make the shared library that the java class needs. (i dont have a makefile yet) g++ -shared -c -I/home/usr/JDK6_4_dev/jdk1.6.0_04/include -I/home/usr/JDK6_4_dev/jdk1.6.0_04/include/linux HelloWorld.cpp -o libHelloWorldImp.so *NOTE - instead of putting java in /usr/lib i am just putting it in home for now to test(since i dont have access to /usr/lib anyway. 6. i perform an -ls and here are the results: HelloWorld.class HelloWorld.cpp HelloWorld.h HelloWorld.java libHelloWorldImp.so 7. now run the program. $java HelloWorld result: Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/usr/JNI_test/libHelloWorldImp.so: /home/usr/JNI_test/libHelloWorldImp.so: only ET_DYN and ET_EXEC can be loaded at java.lang.ClassLoader$NativeLibrary.load(Native Method) at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1751) at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1676) at java.lang.Runtime.loadLibrary0(Runtime.java:823) at java.lang.System.loadLibrary(System.java:1030) at HelloWorld.<clinit>(HelloWorld.java:6) -- ok so can anyone tell me what ET_DYN and ET_EXEC is? note that i am jumping into java here with no experience and am glad that i have gotten this far. any suggestions? thanks a lot in advance!

Share this post


Link to post
Share on other sites
It doesn't find the library in your LD_LIBRARY_PATH I think.

maybe this helps:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${JAVA_HOME}/lib:.

or put your library in one of the directories that are indicated by your former LD_LIBRARY path. That actually seems better than adding current directory to your path in general, like the example above does.

Not quite sure if this will really help, it's been a while that I used JNI. It's a can of worms sometimes.

Good luck :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this