zoukankan      html  css  js  c++  java
  • Jace 一种方便的 JNI机制

     

    为了实现 C++ 调用 Java的函数,以下方法可供参考。https://www.xingdaili.com/browse.php?u=RDFxE%2Bw3stMHuV6Q%2BLJWhbnh4Fb4JxGnYdxt1UkqyGFlfDtFncjT9tbFhUZUE6%2BW26h1OWzBt06FC4GH6pS4VY6iXDkrt6DXpPJosrmDOfQVVzgDl7rvjPaA6LZFYMTgWo73wlSBrNr4G7M%3D&b=6

    Use JNI
    Use Jace
    Use JunC++ion
    Use execl("java", "java", "-jar", "myprog.jar", NULL);
    Use execlp("java", "java", "-jar", "myprog.jar", (char *)0);
    Use system("java filename.jar");
    Use popen("java -jar test.jar text1 text2", "r");
    Use CreateProcess(...);
    Use JNA

    I'm trying to call / execute a java jar from a C++ program.

    Here are the options I've found so far:

    Use JNI
    Use Jace
    Use JunC++ion
    Use execl("java", "java", "-jar", "myprog.jar", NULL);
    Use execlp("java", "java", "-jar", "myprog.jar", (char *)0);
    Use system("java filename.jar");
    Use popen("java -jar test.jar text1 text2", "r");
    Use CreateProcess(...);
    Use JNA
    

    I'd like to use JNI, however I'm running into problems.

    ========================

    Hello.cpp

    I have a simple Hello.cpp class:

    #include <iostream>
    #include <jni.h>
    
    using namespace std;
    
    int main() {
          cout << "Hello World" << endl; // prints Hello World
    
          JavaVM *jvm;       /* denotes a Java VM */
          JNIEnv *env;       /* pointer to native method interface */
          JDK1_1InitArgs vm_args; /* JDK 1.1 VM initialization arguments */
          vm_args.version = 0x00010001; /* New in 1.1.2: VM version */
          /* Get the default initialization arguments and set the class
           * path */
          JNI_GetDefaultJavaVMInitArgs(&vm_args);
          vm_args.classpath = "/home/FinishedJars/HelloWorld/hello2.jar";
          /* load and initialize a Java VM, return a JNI interface
           * pointer in env */
          JNI_CreateJavaVM(&jvm, &env, &vm_args);
          /* invoke the Main.test method using the JNI */
          jclass cls = env->FindClass("Main");
          jmethodID mid = env->GetStaticMethodID(cls, "test", "(I)V");
          env->CallStaticVoidMethod(cls, mid, 100);
          /* We could have created an Object and called methods on it instead */
          /* We are done. */
          jvm->DestroyJavaVM();
    
        return 0;
    }
    

    ========================

    Compile

    And when I try to compile with something like:

    g++ -I /usr/lib/jvm/jdk1.6.0_34-x86/include/ Hello.cpp
    

    Note: I found "jni.h" in that folder, so I included it. I'm assuming that's the right spot, but I'm not certain

    I get a ton of errors ( see below )

  • 相关阅读:
    微信小程序入门
    webpack
    模块化开发(1)
    HTML5表单
    移动端入门
    MySQL
    js面向对象与PHP面向对象总结
    PHP
    Git指令
    Redux
  • 原文地址:https://www.cnblogs.com/cy163/p/3078807.html
Copyright © 2011-2022 走看看