简体   繁体   中英

How to create java class in jni folder? is it possible?

I want to hide my java code.So i crate java class inside jni folder and trying to call that class method.But I'm getting ClassNotFound exception while find class in c++.is it possible?.This is my first ndk project.so i don't know about this.Please tell me solution.

this is my c++ code

#include <jni.h>
#include <string.h>
#include <iostream>
using namespace std;

extern "C" JNIEXPORT jstring JNICALL 
Java_com_example_sohamsaa_ndktest_MyNativeClass_startRecord(JNIEnv* env,jobject obj){

jclass cls2 = env->FindClass("jni/MyTest");//here i'm getting classnotfound exception  

if(cls2 == NULL) {
   cerr << "ERROR: class not found !";
}
else {                                 
   cout << "Class MyTest found" << endl;
   jmethodID mid = env->GetStaticMethodID(cls2, "mymain", "()V"); 
   if(mid == NULL)
       cerr << "ERROR: method void mymain() not found !" << endl;
   else {
       env->CallStaticVoidMethod(cls2, mid);                     
       cout << endl;
   }
}
return (*env).NewStringUTF("Hi siddharthan");
}

this is my java class

public class MyTest {
   public static void mymain() {  
      System.out.println("Hello, World in java from mymain");
  }
}

In theory, yes you could put a class in the JNI folder. However, it would not achieve the result that you desire:

  1. The Android runtime (the classloader) would not be able to find the class, so you would get a runtime exception when your app attempted to load it.
  2. The person who owns the device on which your app has been installed would be able to find the class. It will be stored in the file system somewhere.

Basically, it won't work. You cannot hide the code if you want the user's device to be able to run it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM