Article From:https://segmentfault.com/q/1010000015620207
Question:

c++The process starts the JVM virtual machine and gets the Java related class method.FindClass), If only one class name is successful, if there is a package, it will fail to call. The code is as follows:

//MyTest.java
package com;
import  java.lang.management.ManagementFactory;

public class MyTest {
    //private static int magic_counter = 777;

    public static boolean start(String path) {
        System.out.print("MyTest start: " + path);
        return true;
    }
}
// main.cpp
#include <iostream>
#include <jni.h>
#include <memory.h>

int main()
{
    char opt1[] = "-Djava.compiler=NONE";
    char opt2[] = "-Djava.class.path=./MyTest.jar";
    //char opt2[] = "-Djava.class.path=.";
    char opt3[] = "-verbose:NONE";

    JavaVMOption options[3];
    options[0].optionString = opt1; options[0].extraInfo = NULL;
    options[1].optionString = opt2; options[1].extraInfo = NULL;
    options[2].optionString = opt3; options[2].extraInfo = NULL;

    JavaVMInitArgs jargv;
    jargv.version = JNI_VERSION_1_6;
    jargv.nOptions = 3;
    jargv.options = options;
    jargv.ignoreUnrecognized = JNI_TRUE;

    JavaVM* jvm = NULL;
    JNIEnv* jenv = NULL;
    jint res = JNI_CreateJavaVM( &jvm, (void**)&jenv, &jargv  );
    if ( 0 != res  )
        return 1;

    jclass jc = jenv->FindClass("/com/MyTest");
    //jclass jc = jenv->FindClass("MyTest");
    //jclass jc = jenv->FindClass("java/lang/String");
    if ( NULL == jc  ) {
        if( jenv->ExceptionOccurred()  )
            jenv->ExceptionDescribe();
        else {
            std::cout << "jc null" << std::endl;
        }
        return 1;
    }

    jmethodID jmid = jenv->GetStaticMethodID( jc, "start", "(Ljava/lang/String;)Z");
    if ( NULL == jmid) {
        std::cout << "jmid null" << std::endl;
        return 1;
    }
    else {
        std::cout << "jmid ok" << std::endl;
    }

    jstring jniStr = jenv->NewStringUTF("jni to java");
    jboolean res1 = jenv->CallBooleanMethod( jc, jmid, jniStr);
    if(res1) {
        std::cout << "CallBooleanMethod ok" << std::endl;
    }

    std::cout << "\n";
    std::cout << "Hello, World!" << std::endl;
        return 0;
}

The following orders are as follows:

javac MyTest.java
jar cvf MyTest.jar MyTest.class
g++ main.cpp -I
~/jdk/include/ -I~/jdk/include/linux -L~s/jdk/jre/lib/amd64/server -ljvm

This is running a.out. There will be a false prompt: the class has not been found:
Exception in thread “main” java.lang.NoClassDefFoundError: /com/MyTest
Caused by: java.lang.ClassNotFoundException: .com.MyTest

If the package name com is removed in Java, the class is directly found in c++.MyTest,thatFindClassIt’s a success.

Environment: Ubuntu 16.04 JDK 1.8

Answer 0:

It has been solved.
javaFor the class with registration, we need to find the path to find it.
mkdir com
mv MyTest.jar com/
tar cvf MyTest.jar com

Leave a Reply

Your email address will not be published. Required fields are marked *