2013-10-09 41 views
0

我正在使用JNI从C++访问Java类,但FindClass总是返回nil,只要我在我的java文件中调用某个方法(请参阅java代码)。当我删除通话时,它可以正常工作。来自C++,FindClass和import的JNI调用

./testjnisamplecode

结果:与graph = new OrientGraphNoTx(odb);在Java代码中...

No ! 

结果:没有graph = new OrientGraphNoTx(odb);在Java代码中...

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"); 

} 

的Java

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(); 
    } 
} 

我怎样才能解决这个问题???任何想法?

+1

听起来像一个类加载器问题(即没有找到OrientGraphNoTx类)。在详细打开的情况下,您应该在失败的FindClass调用中看到发送到stderr的内容。你也可以检查异常('ExceptionCheck'和'ExceptionDescribe') – Samhain

回答

0

我认为被遗漏在classpath中orientdb罐子,是不够的蓝图API,你也需要包括东方commons.jar和orientdb-core.jar添加(蓝图 - 定位 - 图依赖性)

再见