.. CLOSED .. JNI error 'java_class == null' Android Studio











up vote
1
down vote

favorite












This is closed. New problem will be addressed in a new question.



See edit for latest problem. I am trying to pass a Vector3 value from my cpp library to my java activity. I am able to do it vice versa, but cannot seem to find a way to go cpp to java. Anyone mine helping me out with this? I am receving this error: undefined reference to 'jni_createjavavm'



JavaVM *jvm;       /* denotes a Java VM */
JNIEnv *env; /* pointer to native method interface */
JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
JavaVMOption* options = new JavaVMOption[1];
options[0].optionString = "-Djava.class.path=/usr/lib/java";
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = false;
/* load and initialize a Java VM, return a JNI interface
* pointer in env */
JNI_CreateJavaVM(&jvm, &env, &vm_args);
delete options;
/* invoke the Main.test method using the JNI */
jclass cls = env->FindClass("MenuActivity");
jmethodID mid = env->GetStaticMethodID(cls, "Test", "(I)V");
env->CallStaticVoidMethod(cls, mid);
/* We are done. */
jvm->DestroyJavaVM();


Nov 11 2018 @2031 UTC+9 | EDIT: New Problem.. Crashes with java_class == NULL.



JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
//Some Other Code Not Regarding JVM

JNIEnv *env;
vm->AttachCurrentThread(&env, NULL);
jclass cls = env->FindClass("MenuActivity");
jmethodID mid = env->GetStaticMethodID(cls, "Test", "(I)V");
env->CallStaticVoidMethod(cls, mid);
return JNI_VERSION_1_6;
}









share|improve this question
























  • Why are you trying to create a JVM instead of just using the one that you got in JNI_OnLoad?
    – Michael
    Nov 10 at 14:15










  • This is my first time even performing this.
    – Liquified Modding
    Nov 10 at 16:23










  • In this case, there are two problems with your updatd JNI_OnLoad. 1) you cannot rely on MenuActivity class to be available when your library is loaded. Don't depend on delicate timing that is controlled by the framework that is beyond your control. 2) You must specify fully qualified name of the class in a call to FindClass(), e.g. env->FindClass("com/example/hellojni/MenuActivity"). See the JniTips again for more details.
    – Alex Cohn
    2 days ago










  • @AlexCohn "you cannot rely on MenuActivity class to be available when your library is loaded." Why would it not be available? The JniTips page even suggest resolving all classes in JNI_OnLoad.
    – Michael
    2 days ago










  • @Michael, you are right. Resolving FindClass from attached thread may not work as expected, and @fadden wisely suggests doing that from JNI_OnLoad as one of alternative workarounds. My concern was more about calling the static MenuActivity.Test(int).
    – Alex Cohn
    2 days ago















up vote
1
down vote

favorite












This is closed. New problem will be addressed in a new question.



See edit for latest problem. I am trying to pass a Vector3 value from my cpp library to my java activity. I am able to do it vice versa, but cannot seem to find a way to go cpp to java. Anyone mine helping me out with this? I am receving this error: undefined reference to 'jni_createjavavm'



JavaVM *jvm;       /* denotes a Java VM */
JNIEnv *env; /* pointer to native method interface */
JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
JavaVMOption* options = new JavaVMOption[1];
options[0].optionString = "-Djava.class.path=/usr/lib/java";
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = false;
/* load and initialize a Java VM, return a JNI interface
* pointer in env */
JNI_CreateJavaVM(&jvm, &env, &vm_args);
delete options;
/* invoke the Main.test method using the JNI */
jclass cls = env->FindClass("MenuActivity");
jmethodID mid = env->GetStaticMethodID(cls, "Test", "(I)V");
env->CallStaticVoidMethod(cls, mid);
/* We are done. */
jvm->DestroyJavaVM();


Nov 11 2018 @2031 UTC+9 | EDIT: New Problem.. Crashes with java_class == NULL.



JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
//Some Other Code Not Regarding JVM

JNIEnv *env;
vm->AttachCurrentThread(&env, NULL);
jclass cls = env->FindClass("MenuActivity");
jmethodID mid = env->GetStaticMethodID(cls, "Test", "(I)V");
env->CallStaticVoidMethod(cls, mid);
return JNI_VERSION_1_6;
}









share|improve this question
























  • Why are you trying to create a JVM instead of just using the one that you got in JNI_OnLoad?
    – Michael
    Nov 10 at 14:15










  • This is my first time even performing this.
    – Liquified Modding
    Nov 10 at 16:23










  • In this case, there are two problems with your updatd JNI_OnLoad. 1) you cannot rely on MenuActivity class to be available when your library is loaded. Don't depend on delicate timing that is controlled by the framework that is beyond your control. 2) You must specify fully qualified name of the class in a call to FindClass(), e.g. env->FindClass("com/example/hellojni/MenuActivity"). See the JniTips again for more details.
    – Alex Cohn
    2 days ago










  • @AlexCohn "you cannot rely on MenuActivity class to be available when your library is loaded." Why would it not be available? The JniTips page even suggest resolving all classes in JNI_OnLoad.
    – Michael
    2 days ago










  • @Michael, you are right. Resolving FindClass from attached thread may not work as expected, and @fadden wisely suggests doing that from JNI_OnLoad as one of alternative workarounds. My concern was more about calling the static MenuActivity.Test(int).
    – Alex Cohn
    2 days ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











This is closed. New problem will be addressed in a new question.



See edit for latest problem. I am trying to pass a Vector3 value from my cpp library to my java activity. I am able to do it vice versa, but cannot seem to find a way to go cpp to java. Anyone mine helping me out with this? I am receving this error: undefined reference to 'jni_createjavavm'



JavaVM *jvm;       /* denotes a Java VM */
JNIEnv *env; /* pointer to native method interface */
JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
JavaVMOption* options = new JavaVMOption[1];
options[0].optionString = "-Djava.class.path=/usr/lib/java";
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = false;
/* load and initialize a Java VM, return a JNI interface
* pointer in env */
JNI_CreateJavaVM(&jvm, &env, &vm_args);
delete options;
/* invoke the Main.test method using the JNI */
jclass cls = env->FindClass("MenuActivity");
jmethodID mid = env->GetStaticMethodID(cls, "Test", "(I)V");
env->CallStaticVoidMethod(cls, mid);
/* We are done. */
jvm->DestroyJavaVM();


Nov 11 2018 @2031 UTC+9 | EDIT: New Problem.. Crashes with java_class == NULL.



JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
//Some Other Code Not Regarding JVM

JNIEnv *env;
vm->AttachCurrentThread(&env, NULL);
jclass cls = env->FindClass("MenuActivity");
jmethodID mid = env->GetStaticMethodID(cls, "Test", "(I)V");
env->CallStaticVoidMethod(cls, mid);
return JNI_VERSION_1_6;
}









share|improve this question















This is closed. New problem will be addressed in a new question.



See edit for latest problem. I am trying to pass a Vector3 value from my cpp library to my java activity. I am able to do it vice versa, but cannot seem to find a way to go cpp to java. Anyone mine helping me out with this? I am receving this error: undefined reference to 'jni_createjavavm'



JavaVM *jvm;       /* denotes a Java VM */
JNIEnv *env; /* pointer to native method interface */
JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
JavaVMOption* options = new JavaVMOption[1];
options[0].optionString = "-Djava.class.path=/usr/lib/java";
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = false;
/* load and initialize a Java VM, return a JNI interface
* pointer in env */
JNI_CreateJavaVM(&jvm, &env, &vm_args);
delete options;
/* invoke the Main.test method using the JNI */
jclass cls = env->FindClass("MenuActivity");
jmethodID mid = env->GetStaticMethodID(cls, "Test", "(I)V");
env->CallStaticVoidMethod(cls, mid);
/* We are done. */
jvm->DestroyJavaVM();


Nov 11 2018 @2031 UTC+9 | EDIT: New Problem.. Crashes with java_class == NULL.



JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
//Some Other Code Not Regarding JVM

JNIEnv *env;
vm->AttachCurrentThread(&env, NULL);
jclass cls = env->FindClass("MenuActivity");
jmethodID mid = env->GetStaticMethodID(cls, "Test", "(I)V");
env->CallStaticVoidMethod(cls, mid);
return JNI_VERSION_1_6;
}






android jni






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago

























asked Nov 10 at 12:00









Liquified Modding

198




198












  • Why are you trying to create a JVM instead of just using the one that you got in JNI_OnLoad?
    – Michael
    Nov 10 at 14:15










  • This is my first time even performing this.
    – Liquified Modding
    Nov 10 at 16:23










  • In this case, there are two problems with your updatd JNI_OnLoad. 1) you cannot rely on MenuActivity class to be available when your library is loaded. Don't depend on delicate timing that is controlled by the framework that is beyond your control. 2) You must specify fully qualified name of the class in a call to FindClass(), e.g. env->FindClass("com/example/hellojni/MenuActivity"). See the JniTips again for more details.
    – Alex Cohn
    2 days ago










  • @AlexCohn "you cannot rely on MenuActivity class to be available when your library is loaded." Why would it not be available? The JniTips page even suggest resolving all classes in JNI_OnLoad.
    – Michael
    2 days ago










  • @Michael, you are right. Resolving FindClass from attached thread may not work as expected, and @fadden wisely suggests doing that from JNI_OnLoad as one of alternative workarounds. My concern was more about calling the static MenuActivity.Test(int).
    – Alex Cohn
    2 days ago


















  • Why are you trying to create a JVM instead of just using the one that you got in JNI_OnLoad?
    – Michael
    Nov 10 at 14:15










  • This is my first time even performing this.
    – Liquified Modding
    Nov 10 at 16:23










  • In this case, there are two problems with your updatd JNI_OnLoad. 1) you cannot rely on MenuActivity class to be available when your library is loaded. Don't depend on delicate timing that is controlled by the framework that is beyond your control. 2) You must specify fully qualified name of the class in a call to FindClass(), e.g. env->FindClass("com/example/hellojni/MenuActivity"). See the JniTips again for more details.
    – Alex Cohn
    2 days ago










  • @AlexCohn "you cannot rely on MenuActivity class to be available when your library is loaded." Why would it not be available? The JniTips page even suggest resolving all classes in JNI_OnLoad.
    – Michael
    2 days ago










  • @Michael, you are right. Resolving FindClass from attached thread may not work as expected, and @fadden wisely suggests doing that from JNI_OnLoad as one of alternative workarounds. My concern was more about calling the static MenuActivity.Test(int).
    – Alex Cohn
    2 days ago
















Why are you trying to create a JVM instead of just using the one that you got in JNI_OnLoad?
– Michael
Nov 10 at 14:15




Why are you trying to create a JVM instead of just using the one that you got in JNI_OnLoad?
– Michael
Nov 10 at 14:15












This is my first time even performing this.
– Liquified Modding
Nov 10 at 16:23




This is my first time even performing this.
– Liquified Modding
Nov 10 at 16:23












In this case, there are two problems with your updatd JNI_OnLoad. 1) you cannot rely on MenuActivity class to be available when your library is loaded. Don't depend on delicate timing that is controlled by the framework that is beyond your control. 2) You must specify fully qualified name of the class in a call to FindClass(), e.g. env->FindClass("com/example/hellojni/MenuActivity"). See the JniTips again for more details.
– Alex Cohn
2 days ago




In this case, there are two problems with your updatd JNI_OnLoad. 1) you cannot rely on MenuActivity class to be available when your library is loaded. Don't depend on delicate timing that is controlled by the framework that is beyond your control. 2) You must specify fully qualified name of the class in a call to FindClass(), e.g. env->FindClass("com/example/hellojni/MenuActivity"). See the JniTips again for more details.
– Alex Cohn
2 days ago












@AlexCohn "you cannot rely on MenuActivity class to be available when your library is loaded." Why would it not be available? The JniTips page even suggest resolving all classes in JNI_OnLoad.
– Michael
2 days ago




@AlexCohn "you cannot rely on MenuActivity class to be available when your library is loaded." Why would it not be available? The JniTips page even suggest resolving all classes in JNI_OnLoad.
– Michael
2 days ago












@Michael, you are right. Resolving FindClass from attached thread may not work as expected, and @fadden wisely suggests doing that from JNI_OnLoad as one of alternative workarounds. My concern was more about calling the static MenuActivity.Test(int).
– Alex Cohn
2 days ago




@Michael, you are right. Resolving FindClass from attached thread may not work as expected, and @fadden wisely suggests doing that from JNI_OnLoad as one of alternative workarounds. My concern was more about calling the static MenuActivity.Test(int).
– Alex Cohn
2 days ago












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










On Android, there is no JNI_CreateJavaVM(). The apps run in JVM which is essential to access system APIs and services.



The callbacks from native code to the Java part of the app use the JNIEnv * that must belong to the current thread.



If this runs on a Java thread, the JNIEnv is received as the first parameter by the native method. You can call back to Java from a native thread, too. But then, you must attach the thread to JVM. AttachCurrentThread() accepts JavaVM * which can be stored as a global in your native code. You can obtain it in JNI_OnLoad() or derive it from JNIEnv with GetJavaVM().



Each native thread that is attached, must be detached on termination. The best practice is to use pthread_key_create() to define a destructor function that will be called before the thread exits.



You can read more explanations in the Android JNI tips article.






share|improve this answer























  • Thank you for that answer. I have a new problem now, and I would like some assistance if you may. Thank you.
    – Liquified Modding
    2 days ago










  • generally speaking, it's preferrable to open a separate question when a new problem pops up, even if it's a follow up for an accepted answer.
    – Alex Cohn
    2 days ago










  • Good to go. Thanks for your help!
    – Liquified Modding
    2 days ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238723%2fclosed-jni-error-java-class-null-android-studio%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










On Android, there is no JNI_CreateJavaVM(). The apps run in JVM which is essential to access system APIs and services.



The callbacks from native code to the Java part of the app use the JNIEnv * that must belong to the current thread.



If this runs on a Java thread, the JNIEnv is received as the first parameter by the native method. You can call back to Java from a native thread, too. But then, you must attach the thread to JVM. AttachCurrentThread() accepts JavaVM * which can be stored as a global in your native code. You can obtain it in JNI_OnLoad() or derive it from JNIEnv with GetJavaVM().



Each native thread that is attached, must be detached on termination. The best practice is to use pthread_key_create() to define a destructor function that will be called before the thread exits.



You can read more explanations in the Android JNI tips article.






share|improve this answer























  • Thank you for that answer. I have a new problem now, and I would like some assistance if you may. Thank you.
    – Liquified Modding
    2 days ago










  • generally speaking, it's preferrable to open a separate question when a new problem pops up, even if it's a follow up for an accepted answer.
    – Alex Cohn
    2 days ago










  • Good to go. Thanks for your help!
    – Liquified Modding
    2 days ago















up vote
1
down vote



accepted










On Android, there is no JNI_CreateJavaVM(). The apps run in JVM which is essential to access system APIs and services.



The callbacks from native code to the Java part of the app use the JNIEnv * that must belong to the current thread.



If this runs on a Java thread, the JNIEnv is received as the first parameter by the native method. You can call back to Java from a native thread, too. But then, you must attach the thread to JVM. AttachCurrentThread() accepts JavaVM * which can be stored as a global in your native code. You can obtain it in JNI_OnLoad() or derive it from JNIEnv with GetJavaVM().



Each native thread that is attached, must be detached on termination. The best practice is to use pthread_key_create() to define a destructor function that will be called before the thread exits.



You can read more explanations in the Android JNI tips article.






share|improve this answer























  • Thank you for that answer. I have a new problem now, and I would like some assistance if you may. Thank you.
    – Liquified Modding
    2 days ago










  • generally speaking, it's preferrable to open a separate question when a new problem pops up, even if it's a follow up for an accepted answer.
    – Alex Cohn
    2 days ago










  • Good to go. Thanks for your help!
    – Liquified Modding
    2 days ago













up vote
1
down vote



accepted







up vote
1
down vote



accepted






On Android, there is no JNI_CreateJavaVM(). The apps run in JVM which is essential to access system APIs and services.



The callbacks from native code to the Java part of the app use the JNIEnv * that must belong to the current thread.



If this runs on a Java thread, the JNIEnv is received as the first parameter by the native method. You can call back to Java from a native thread, too. But then, you must attach the thread to JVM. AttachCurrentThread() accepts JavaVM * which can be stored as a global in your native code. You can obtain it in JNI_OnLoad() or derive it from JNIEnv with GetJavaVM().



Each native thread that is attached, must be detached on termination. The best practice is to use pthread_key_create() to define a destructor function that will be called before the thread exits.



You can read more explanations in the Android JNI tips article.






share|improve this answer














On Android, there is no JNI_CreateJavaVM(). The apps run in JVM which is essential to access system APIs and services.



The callbacks from native code to the Java part of the app use the JNIEnv * that must belong to the current thread.



If this runs on a Java thread, the JNIEnv is received as the first parameter by the native method. You can call back to Java from a native thread, too. But then, you must attach the thread to JVM. AttachCurrentThread() accepts JavaVM * which can be stored as a global in your native code. You can obtain it in JNI_OnLoad() or derive it from JNIEnv with GetJavaVM().



Each native thread that is attached, must be detached on termination. The best practice is to use pthread_key_create() to define a destructor function that will be called before the thread exits.



You can read more explanations in the Android JNI tips article.







share|improve this answer














share|improve this answer



share|improve this answer








answered 2 days ago


























community wiki





Alex Cohn













  • Thank you for that answer. I have a new problem now, and I would like some assistance if you may. Thank you.
    – Liquified Modding
    2 days ago










  • generally speaking, it's preferrable to open a separate question when a new problem pops up, even if it's a follow up for an accepted answer.
    – Alex Cohn
    2 days ago










  • Good to go. Thanks for your help!
    – Liquified Modding
    2 days ago


















  • Thank you for that answer. I have a new problem now, and I would like some assistance if you may. Thank you.
    – Liquified Modding
    2 days ago










  • generally speaking, it's preferrable to open a separate question when a new problem pops up, even if it's a follow up for an accepted answer.
    – Alex Cohn
    2 days ago










  • Good to go. Thanks for your help!
    – Liquified Modding
    2 days ago
















Thank you for that answer. I have a new problem now, and I would like some assistance if you may. Thank you.
– Liquified Modding
2 days ago




Thank you for that answer. I have a new problem now, and I would like some assistance if you may. Thank you.
– Liquified Modding
2 days ago












generally speaking, it's preferrable to open a separate question when a new problem pops up, even if it's a follow up for an accepted answer.
– Alex Cohn
2 days ago




generally speaking, it's preferrable to open a separate question when a new problem pops up, even if it's a follow up for an accepted answer.
– Alex Cohn
2 days ago












Good to go. Thanks for your help!
– Liquified Modding
2 days ago




Good to go. Thanks for your help!
– Liquified Modding
2 days ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238723%2fclosed-jni-error-java-class-null-android-studio%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation