2013-10-09 46 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添加(藍圖 - 定位 - 圖依賴性)

再見