使用JNI在C ++中加载.jar文件 [英] Load .jar file in c++ using JNI

查看:114
本文介绍了使用JNI在C ++中加载.jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将c ++(OpenCV项目)与Java代码(Web服务程序)集成在一起,所以我正在使用JNI对其进行集成.

I need to integrate my c++ (OpenCV project) with Java code (a web service program) so i am using JNI for integrating them.

我可以使用JNI从C ++运行helloWorld Java代码,如下所示:

I can run my helloWorld java code from c++ by using JNI as you can see in the below:

import java.io.IOException;

public class Main {

    public Main() {
        System.out.println("hello from no arg");
    }

    public static void main(String[] args) throws IOException {     

    }

    private static void callRestPost2(String ontologyURI, String object, String predicate, String subject) throws IOException {
            java.util.Map<String, Object> params = new java.util.HashMap<String, Object>();//hedef
            params.put("operationType", "insertTriple4TrafficOntology");
            params.put("ontologyURI", ontologyURI);
            params.put("object", object);
            params.put("predicate", predicate);
            params.put("subject", subject);
            System.out.println("Hello World!"); 
     }
}

但是我无法使用JNI从C ++运行具有.jar依赖关系的Java代码,它在第25行给出了错误(我猜是由于spring框架中的新对象[spring-web-4.2.4 .RELEASE.jar])创建在第25行).

But I can't run my java code which has .jar dependency, from C++ by using JNI, it gives an error on 25th line (I guess because of the new object from spring framework [spring-web-4.2.4.RELEASE.jar]) is created on 25th line).

import java.io.IOException;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public class Main {

    public Main() {
        System.out.println("hello from no arg");
    }

    public static void main(String[] args) throws IOException {     

    }

    private static void callRestPost2(String ontologyURI, String object, String predicate, String subject) throws IOException {
            java.util.Map<String, Object> params = new java.util.HashMap<String, Object>();//hedef
            params.put("operationType", "insertTriple4TrafficOntology");
            params.put("ontologyURI", ontologyURI);
            params.put("object", object);
            params.put("predicate", predicate);
            params.put("subject", subject);
            ResponseEntity<String> entity = null;
            RestTemplate restTemplate = new RestTemplate();
            entity = restTemplate.exchange("http://www.mantam.com.tr/c3po-rest-service/c3pont/insertTriple4TrafficOntology" + "?ontologyURI={ontologyURI}&subject={subject}&object={object}&predicate={predicate}",HttpMethod.POST, null, String.class, params);
             System.out.println(entity.toString());     
    }
}

结果,我的问题是如何使用JNI在c ++中加载.jar文件?"

As a result, my question is that "How to load .jar file in c++ using JNI?"

我的C ++代码:

#include <jni.h>

int main()
{
   Using namespace std;
   JavaVM *jvm;                      // Pointer to the JVM (Java Virtual Machine)
   JNIEnv *env;                      // Pointer to native interface
       //================== prepare loading of Java VM ============================
   JavaVMInitArgs vm_args;                        // Initialization arguments
   JavaVMOption* options = new JavaVMOption[1];   // JVM invocation options
   options[0].optionString = "-Djava.class.path=C:\\Users\\proje\\workspace\\HelloWorld\\bin";   // where to find java .class
   vm_args.version = JNI_VERSION_1_6;             // minimum Java version
   vm_args.nOptions = 1;                          // number of options
   vm_args.options = options;
   vm_args.ignoreUnrecognized = false;     // invalid options make the JVM init fail
       //=============== load and initialize Java VM and JNI interface =============
   jint rc = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);  // YES !!
   delete options;    // we then no longer need the initialisation options. 
   if (rc != JNI_OK) {
          // TO DO: error processing... 
         cin.get();
         exit(EXIT_FAILURE);
   }
      //=============== Display JVM version =======================================
   cout << "JVM load succeeded: Version ";
   jint ver = env->GetVersion();
   cout << ((ver>>16)&0x0f) << "."<<(ver&0x0f) << endl;


    jclass cls2 = env->FindClass("Main");  // try to find the class
    if (cls2 == nullptr) {
        cerr << "ERROR: class not found !";
    }
    else {                                  // if class found, continue
        cout << "Class MyTest found" << endl;
        jmethodID mid2 = env->GetStaticMethodID(cls2, "callRestPost2", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
        if (mid2 == nullptr) {
            cerr << "ERROR: method it main2(int) not found !" << endl;
        }
        else {
            jstring jstr1 = env->NewStringUTF("http://traffic.c3po.mantis.com.tr");
            jstring jstr2 = env->NewStringUTF(c);
            jstring jstr3 = env->NewStringUTF("measured_At");
            jstring jstr4 = env->NewStringUTF("Zevkli_Sokak");
            env->CallStaticVoidMethod(cls2, mid2, jstr1, jstr2, jstr3, jstr4);
            cout << endl;
        }
    }

   jvm->DestroyJavaVM();
   cin.get();
}

推荐答案

我找到了解决方案,现在很高兴:)

I found the solution and feel happy now :)

您应将完整路径添加到.jar文件.例如:

You should add the complete path to your .jar files. For example:

options = new JavaVMOption[1];   // JVM invocation options
options[0].optionString =   "Djava.class.path=C:\\Users\\proje\\workspace\\HelloWorld\\bin;C:\\Users\\proje\\workspace\\HelloWorld\\bin\\spring-web-4.2.4.RELEASE.jar;C:\\Users\\proje\\workspace\\HelloWorld\\bin\\spring-core-4.2.4.RELEASE.jar;C:\\Users\\proje\\workspace\\HelloWorld\\bin\\spring-beans-4.2.4.RELEASE.jar;C:\\Users\\proje\\workspace\\HelloWorld\\bin\\commons-logging-1.2.jar";   // where to find java .class and .jar files

就是这样!

这篇关于使用JNI在C ++中加载.jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆