简体   繁体   English

如何在jni文件夹中创建Java类? 可能吗?

[英]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. 我想隐藏我的Java代码。所以我在jni文件夹中创建了java类并试图调用该类方法。但是我在c ++中找到类时却遇到ClassNotFound异常,这可能吗?。这是我的第一个ndk project.so我对此一无所知。请告诉我解决方法。

this is my c++ code 这是我的C ++代码

#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 这是我的java课

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. 从理论上讲,可以将一个类放在JNI文件夹中。 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. Android运行时(类加载器)无法找到该类,因此当您的应用尝试加载它时,您将获得运行时异常。
  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. 如果希望用户的设备能够运行该代码,则无法隐藏该代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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