我正在做一个示例程序来从java调用c ++程序。我已经完成了Java JNI框架并遵循了一些示例。我正在AIX 5 powerpc系统中实现示例。 这是我的java代码片段。
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 from java");
hello.helloCpp();
System.out.println("retrieve value from c++");
int i_perf_obj=hello.getPerfObj();
System.out.println("i_perf_obj value from c++ program"+i_perf_obj);
}
}
通过编译从上面生成头文件。 下面是带有OCCI数据库连接实现的c ++程序,它是从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 FROM emp where 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 for displayAllRows"<<endl;
cout<<"Error number: "<< ex.getErrorCode() << endl;
cout<<ex.getMessage() << endl;
}
stmt->closeResultSet (rset);
conn->terminateStatement (stmt);
cout << "Terminating the connection......" << endl;
env->terminateConnection (conn);
cout << "Connection Terminated" << endl;
Environment::terminateEnvironment (env);
cout << "Environment Terminated" << endl;
cout<<"returning value from c++ program";
return empid;
}
int main(){}
我使用以下命令编译了c ++程序。
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
我生成了共享库,如下所示。
xlC -q64 -G -qmkshrobj JavaToCpp.o -o JavaToCpp.so
当我运行java程序时
java HelloWorld
我收到以下错误。
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.
The module has an invalid magic number.)
at java.lang.ClassLoader.loadLibraryWithPath(ClassLoader.java:995)
at java.lang.System.load(System.java:441)
at Helloworld.main(Helloworld.java:16)
任何人都可以帮我解决这个问题。
提前致谢。