ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ### linux下安装java环境/c/c++编译环境 Dockerfile ``` From centos:7 RUN yum install -y java-1.8.0-openjdk-devel.x86_64 \ net-tools \ gcc \ gcc-c++ RUN export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.4.11-0.el7_6.x86_64/ \ && export PATH=$JAVA_HOME/bin:$PATH ``` ### c/c++代码 ``` #include <stdio.h> #include <iostream> #include <jni.h> #include <stdlib.h> #include <assert.h> #include <string> #include <string.h> using namespace std; int main() { JavaVMOption options[2]; JNIEnv *env; JavaVM *jvm; JavaVMInitArgs vm_args; long status; jclass cls; jmethodID mid; jint square; jboolean jnot; jobject jobj; options[0].optionString = "-Djava.compiler=NONE"; options[1].optionString = "-Djava.class.path=./ActiveForIMServer.jar"; //jar包的位置 //options[2].optionString = "-verbose:jni"; //用于跟踪运行时的信息 vm_args.version = JNI_VERSION_1_8; // JDK版本号 vm_args.nOptions = 2; vm_args.options = options; vm_args.ignoreUnrecognized = JNI_TRUE; status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); //api : public static boolean InLZSystem() if(status != JNI_ERR){ printf("create java jvm success\n"); cls = env->FindClass("com/xxx/xxx/xxx"); // 在这里查找java类 if(cls !=0){ printf("find java class success\n"); // 构造函数 /*mid = env->GetMethodID(cls,"<init>","()V"); if(mid !=0){ jobj=env->NewObject(cls,mid); std::cout << "init ok" << std::endl; }*/ // 调用InLZSystem函数 mid = env->GetStaticMethodID( cls, "InLZSystem", "()Z"); std::cout << "start" << std::endl; if(mid !=0){ square = env->CallStaticIntMethod( cls, mid); std::cout << square << std::endl; }else { std::cout << "failed" << std::endl; } } else{ fprintf(stderr, "FindClass failed\n"); } jvm->DestroyJavaVM(); fprintf(stdout, "Java VM destory.\n"); return 0; } else{ printf("create java jvm fail\n"); return -1; } } ```