Problems when calling a c++ program from java using JNI -
i doing sample demonstration program call c++ program java. have gone through java jni framework , followed samples. implementing sample in aix 5 powerpc system. here java code snippet.
public class helloworld { public native void hellocpp(); public native int getempid(); public static void main(string[] args) { //system.loadlibrary("occidml"); system.loadlibrary("javatocpp"); // shared object file. helloworld hello=new helloworld(); system.out.println("calling c++ methods java"); hello.hellocpp(); system.out.println("retrieve value c++"); int i_perf_obj=hello.getperfobj(); system.out.println("i_perf_obj value c++ program"+i_perf_obj); } }
generated header file above compiling. below c++ program occi database connection implementation calling java
#include <iostream> #include "helloworld.h" #include <occi.h> using namespace oracle::occi; using namespace std; jniexport void jnicall java_helloworld_hellocpp (jnienv *, jobject) { cout<<"hello world form c++"<<endl; return; } jniexport jint jnicall java_helloworld_getempid (jnienv *env , jobject obj) { cout<<"in side c++ program"; int empid=0; cout<<"calling occi method retreiveperfobj()"; environment *env; connection *conn; statement *stmt; string user = "hr"; string passwd = "admin"; string db = "mydb:1521/mydb"; env = environment::createenvironment(environment::default); cout<<"environment created"<<endl; cout<<"user:"<<user<<"\tpass:"<<passwd<<"\tdb:"<<db<<endl; conn = env->createconnection (user, passwd, db); cout<<"connection established"<<endl; string sqlstmt = "select id emp emp_id=123"; stmt = conn->createstatement (sqlstmt); resultset *rset = stmt->executequery (); try{ while (rset->next ()) { i_perf_obj=rset->getint (1); cout << "i_perf_obj: "<<i_perf_obj<< "\t\t"<< endl; } }catch(sqlexception ex) { cout<<"exception thrown displayallrows"<<endl; cout<<"error number: "<< ex.geterrorcode() << endl; cout<<ex.getmessage() << endl; } stmt->closeresultset (rset); conn->terminatestatement (stmt); cout << "terminating connection......" << endl; env->terminateconnection (conn); cout << "connection terminated" << endl; environment::terminateenvironment (env); cout << "environment terminated" << endl; cout<<"returning value c++ program"; return empid; } int main(){}
i compiled c++ program using following command.
make buildocci objs=javatocpp.o exe=javatocpp xlc -c -i/usr/appl/pmr/uat/oci-11.2.0.4.0/instantclient_11_2/sdk/include - i/usr/java5/include -q64 -qlongdouble -i/usr/appl/pmr/uat/oci- 11.2.0.4.0/instantclient_11_2/sdk/include javatocpp.cpp xlc -o javatocpp -q64 javatocpp.o -l/usr/appl/pmr/uat/oci- 11.2.0.4.0/instantclient_11_2/ -locci -lclntsh
and generated shared library below.
xlc -q64 -g -qmkshrobj javatocpp.o -o javatocpp.so
when ran java program
java helloworld
am getting below error.
exception in thread "main" java.lang.unsatisfiedlinkerror: /usr/appl/pmr/uat/temp/jk225/javatocppdemo/javatocpp.so (could not load module /usr/appl/pmr/uat/temp/jk225/javatocppdemo/javatocpp.so. module has invalid magic number.) @ java.lang.classloader.loadlibrarywithpath(classloader.java:995) @ java.lang.system.load(system.java:441) @ helloworld.main(helloworld.java:16)
can please me how solve this.
thanks in advance.
Comments
Post a Comment