我正在使用JNI从C ++访问Java类,但只要我在java文件中调用某个方法,FindClass
总是返回nil
(参见java代码)。当我删除呼叫时,它可以正常工作。
./ testjnisamplecode
结果:在java代码中使用graph = new OrientGraphNoTx(odb);
...
No !
结果:在java代码中没有graph = new OrientGraphNoTx(odb);
...
Yes !
Initializing graph ..
使用命令行的Java,它工作正常:
java -cp "/mypath/orientdb/debug/orientdb-c/lib/*:." Inserter
结果:
Initializing graph ..
Initialized ok ..
C ++
#include <string.h>
#include <stdio.h>
#include <jni.h>
void main()
{
JavaVM *vm;
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption opts[2];
opts[0].optionString = "-Djava.class.path=/mypath/orientdb/debug/orientdb-c:/mypath/orientdb/debug/orientdb-c/lib:/mypath/orientdb/debug/orientdb-c/lib/blueprints-core-2.5.0-SNAPSHOT.jar:/mypath/orientdb/debug/orientdb-c/lib/blueprints-orient-graph-2.5.0-SNAPSHOT.jar:.";
opts[1].optionString = "-verbose:jni";
vm_args.version = JNI_VERSION_1_2;
vm_args.nOptions = 0;
vm_args.ignoreUnrecognized = 1;
jmethodID mid;
jint square;
// Construct a VM
jint res = JNI_CreateJavaVM(&vm, (void **)&env, &vm_args);
// First get the class that contains the method you need to call
jclass clazz = (*env)->FindClass(env, "Inserter");
// TEST IF CLASS WAS FOUND
if (clazz) {
printf("Yes !\n");
}
else {
printf("No !\n");
exit(1);
}
// get init method
jmethodID init = (*env)->GetMethodID(env, clazz, "<init>",
"()V");
}
爪哇
import java.io.IOException;
import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx;
import com.orientechnologies.orient.core.db.graph.OGraphDatabase;
import com.tinkerpop.blueprints.TransactionalGraph;
//import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import java.util.List;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.orientechnologies.orient.core.metadata.schema.OType;
public class Inserter
{
private OGraphDatabase odb;
private OrientGraphNoTx graph;
public Inserter()
{
System.out.println("Initializing graph .. ");
odb = new OGraphDatabase("local:/home/vagrant/orientdb-graphed-1.5.1/databases/sitepoint-ruby-demo").open("admin","admin");
//graph = new OrientGraphNoTx(odb); // <----- THIS BREAKS C++ CODE
//System.out.println("Initialized ok .. ");
}
public static void main(String[] args)
{
Inserter ins = new Inserter();
}
}
我该如何解决这个问题?有什么想法吗?
答案 0 :(得分:0)
我认为错过了classpath中的orientdb jar,是不够的蓝图api,你还需要包括orient-commons.jar和orientdb-core.jar(blueprints-orient-graph dependencies)
再见