Android Question how to run javaCode in b4a?java file to class

xiaoyao

Active Member
Licensed User
Longtime User
    • in windows 7,JavaCompiler ,it can Compiler java code in memory,and run java class.
    • but with same JavaCompiler code ,make jar,and make apk.run app in adnroid ,JavaCompiler code can't work. (may be my code of "JavaCompiler",is win7, can't run in Linux and android.
    • i use dextool.bat ,i can make *.jar to *.dex file。and app load dex to run it ,my question is :
      how to Compiler java code to class format file,and i can add one to more class File TO Make Jar( lik zip file), so make like abc.dex.
 

xiaoyao

Active Member
Licensed User
Longtime User
B4X:
public class Client {
     public static void main(String[] args) throws Exception {
         //Java源代码
         String sourceStr = "public class Hello{    public String sayHello (String name) {return \"Hello,\" + name + \"!\";}}";
         //类名及文件名
         String clsName = "Hello";
         //方法名
         String methodName = "sayHello";
         //当前编译器
         JavaCompiler cmp = ToolProvider.getSystemJavaCompiler();
         //Java标准文件管理器
         StandardJavaFileManager fm = cmp.getStandardFileManager(null,null,null);
         //Java文件对象
         JavaFileObject jfo = new StringJavaObject(clsName,sourceStr);
         //编译参数,类似于javac <options>中的options
         List<String> optionsList = new ArrayList<String>();
         //编译文件的存放地方,注意:此处是为Eclipse工具特设的
         optionsList.addAll(Arrays.asList("-d","./bin"));
         //要编译的单元
         List<JavaFileObject> jfos = Arrays.asList(jfo);
         //设置编译环境
         JavaCompiler.CompilationTask task = cmp.getTask(null, fm, null, optionsList,null,jfos);
         //编译成功
         if(task.call()){
             //生成对象
             Object obj = Class.forName(clsName).newInstance();
             Class<? extends Object> cls = obj.getClass();
             //调用sayHello方法
             Method m = cls.getMethod(methodName, String.class);
             String str = (String) m.invoke(obj, "Dynamic Compilation");
             System.out.println(str);
        }
    }
}
//文本中的Java对象
class StringJavaObject extends SimpleJavaFileObject{
     //源代码
     private String content = "";
     //遵循Java规范的类名及文件
     public StringJavaObject(String _javaFileName,String _content){
           super(_createStringJavaObjectUri(_javaFileName),Kind.SOURCE);
           content = _content;
     }
     //产生一个URL资源路径
     private static URI _createStringJavaObjectUri(String name){
        //注意此处没有设置包名
        return URI.create("String:///" + name + Kind.SOURCE.extension);
     }
     //文本文件代码
     @Override
     public CharSequence getCharContent(boolean ignoreEncodingErrors)
            throws IOException {
        return content;
    }
}
 
Upvote 0

xiaoyao

Active Member
Licensed User
Longtime User
some user is chinese people,so i put chinese word:
  • 怎么在安卓APP运行时把JAVA代码编绎成CLASS格式?目标在安卓APP中直接运行JAVA代码,原理是安卓系统上的APP可以动态加载DEX格式的文件,然后调用其中的功能函数。
  • 我已经有了jar转DEX的功能。具体过程是(1,把JAVA文件编绎成CLASS格式,2,把多个CLASS文件打包进jar,其实就是一个ZIP文件嘛。然后把jar文件转成DEX。)最难的就是第一步,怎么在手机中把JAVA文件转成CLASS格式。
  • WIN7电脑上用JAVAC.EXE或动态编绎功能就可以做到这一步:JavaCompiler cmp = ToolProvider.getSystemJavaCompiler(),如何让这代码在安卓手机中运行成功?
  • 是不是需要下载LINUX版的开源JAVAC,把相关的class编绎成一个JAR文件,专门在手机中让他动态输入JAVA代码,然后生成CLASS格式?
有人说:为什么不考虑远程生成呢?一个class顶天了也就几十k。
搞一个服务器,让安卓APP用户手机上写的JAVA代码,传到服务器,再生成Class文件下载,或者直接生成jar,dex文件,也可以一步生成APK文件。
 
Upvote 0

xiaoyao

Active Member
Licensed User
Longtime User
Why not consider remote generation? A class is up to tens of thousands of K.

Make a server, let Android APP users write JAVA code on their mobile phones, pass it to the server, regenerate to download Class files, or directly generate jar, DEX files, you can also generate APK files step by step.
 
Upvote 0
Top