Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JNI没有';不要在cmake环境中创建JClass_Java_C++_Java Native Interface - Fatal编程技术网

Java JNI没有';不要在cmake环境中创建JClass

Java JNI没有';不要在cmake环境中创建JClass,java,c++,java-native-interface,Java,C++,Java Native Interface,所以,我有一个问题,我有一些代码在通过终端编译时运行,但不是在cmake环境中运行 我想我可以把我的错误减少到以下几点: int main(int argc, char **argv) { jclass whatever; std::cout << "whatever: " << whatever << std::endl; JavaVM * jvm; JNIEnv* env = create_vm(&jvm);

所以,我有一个问题,我有一些代码在通过终端编译时运行,但不是在cmake环境中运行

我想我可以把我的错误减少到以下几点:

int main(int argc, char **argv) {

    jclass whatever;
    std::cout << "whatever: " << whatever << std::endl; 
    JavaVM * jvm;
    JNIEnv* env = create_vm(&jvm);
    invoke_class( env );
}
一般守则如下:

#include <stdio.h>
#include <jni.h>
#include <iostream>

JNIEnv* create_vm(JavaVM ** jvm) {
    JNIEnv* env;
    JavaVMInitArgs vm_args;
    JavaVMOption options;
    vm_args.version = JNI_VERSION_1_6;
    vm_args.nOptions = 1;
    options.optionString = "-Djava.class.path=.";
    vm_args.options = &options;
    vm_args.ignoreUnrecognized = JNI_FALSE;

    int ret = JNI_CreateJavaVM(jvm, (void **)&env, &vm_args);
    if(ret < 0)
        printf("\nUnable to Launch JVM\n");
    return env;
}
void call_testing2(JNIEnv* env, jclass hClass){
    jmethodID testing2Method;
    jfloatArray floats = env->NewFloatArray(1);
    jintArray test = env->NewIntArray(1);
    jfloat *fl = env->GetFloatArrayElements(floats, NULL);
    fl[0] = 5.893241874931;
    env->ReleaseFloatArrayElements(floats, fl, 0);
    jint *in = env->GetIntArrayElements(test, NULL);
    in[0] = 42;
    env->ReleaseIntArrayElements(test, in, 0);
    testing2Method = env->GetMethodID(hClass, "testing2", "([F[I)I");
    int x = env->CallStaticIntMethod(hClass, testing2Method, floats, test);
}
void invoke_class(JNIEnv* env) {
    jclass helloWorldClass;
    helloWorldClass = env->FindClass("InvocationHelloWorld");
    call_testing2(env, helloWorldClass);
}
#包括
#包括
#包括
JNIEnv*创建虚拟机(JavaVM**jvm){
JNIEnv*env;
JavaVMInitArgs vm_args;
JavaVMOption选项;
vm_args.version=JNI_version_1_6;
vm_args.nOptions=1;
options.optionString=“-Djava.class.path=”;
vm_args.options=&options;
vm_args.ignoreunrecogned=JNI_FALSE;
int ret=JNI_CreateJavaVM(jvm,(void**)&env和vm_args);
如果(ret<0)
printf(“\n无法启动JVM\n”);
返回环境;
}
无效调用测试2(JNIEnv*env,jclass hClass){
jmethod试验2方法;
jfloatArray floats=env->NewFloatArray(1);
金塔瑞试验=环境->纽因塔瑞(1);
jfloat*fl=env->GetFloatArrayElements(浮点,NULL);
fl[0]=5.893241874931;
环境->释放浮点数组元素(浮点,fl,0);
jint*in=env->GetIntArrayElements(测试,空);
in[0]=42;
环境->发布安排(测试,输入,0);
testing2Method=env->GetMethodID(hClass,“testing2”,“([F[I)I”);
intx=env->CallStaticIntMethod(hClass,testing2Method,float,test);
}
void invoke_类(JNIEnv*env){
jclass helloWorldClass;
helloWorldClass=env->FindClass(“调用HelloWorld”);
调用_testing2(env,helloWorldClass);
}
jclass“helloWorldClass”也会出现同样的问题。 我已经在cmake文件中包含了jni路径并链接了jni库。这里出了什么问题?

jclass;
jclass whatever;
std::cout << "whatever: " << whatever << std::endl; 

std::cout也许我应该指定我的问题..jclass“helloWorldClass”在helloWorldClass=env->FindClass(“InvocationHelloWorld”);行之后也是0,-因此我似乎根本无法创建或初始化jclass..“似乎根本无法创建或初始化jclass”。听起来更像是您为
FindClass
指定了错误的类路径,或者使用了错误的类装入器。
FindClass
可能会引发异常,您可以使用打印。也许我应该指定我的问题。..jclass“helloWorldClass”在helloWorldClass=env->FindClass(“InvocationHelloWorld”)行之后也是0看来我根本无法创建或初始化jclass。。
jclass whatever;
std::cout << "whatever: " << whatever << std::endl;