.. 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;
}
android jni
|
show 1 more comment
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;
}
android jni
Why are you trying to create a JVM instead of just using the one that you got inJNI_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 inJNI_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 staticMenuActivity.Test(int)
.
– Alex Cohn
2 days ago
|
show 1 more comment
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;
}
android jni
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
android jni
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 inJNI_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 inJNI_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 staticMenuActivity.Test(int)
.
– Alex Cohn
2 days ago
|
show 1 more comment
Why are you trying to create a JVM instead of just using the one that you got inJNI_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 inJNI_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 staticMenuActivity.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
|
show 1 more comment
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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