安卓支持动态编译Java源文件么?像Java平台 :
public static void main(String[] args) throws Exception {
String source =
"public class Main { " +
"public static void main(String[]args) {" +
"System.out.println(\"Hello World!\");" +
"} " +
"}"; JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
StringSourceJavaObject sourceObject = new CompilerTest.StringSourceJavaObject("Main", source);
Iterable<? extends JavaFileObject> fileObjects = Arrays.asList(sourceObject);
CompilationTask task = compiler.getTask(null, fileManager, null, null, null, fileObjects);
boolean result = task.call(); if (result) {
System.out.println("编译成功。");
ClassLoader loader = CompilerTest.class.getClassLoader();
try {
Class<?> clazz = loader.loadClass("Main");
Method method = clazz.getMethod("main", new Class<?>[] {});
Object value = method.invoke(null, new Object[] {});
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
}
}
}求解答.
public static void main(String[] args) throws Exception {
String source =
"public class Main { " +
"public static void main(String[]args) {" +
"System.out.println(\"Hello World!\");" +
"} " +
"}"; JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
StringSourceJavaObject sourceObject = new CompilerTest.StringSourceJavaObject("Main", source);
Iterable<? extends JavaFileObject> fileObjects = Arrays.asList(sourceObject);
CompilationTask task = compiler.getTask(null, fileManager, null, null, null, fileObjects);
boolean result = task.call(); if (result) {
System.out.println("编译成功。");
ClassLoader loader = CompilerTest.class.getClassLoader();
try {
Class<?> clazz = loader.loadClass("Main");
Method method = clazz.getMethod("main", new Class<?>[] {});
Object value = method.invoke(null, new Object[] {});
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
}
}
}求解答.
解决方案 »
- 我的异步线程怎么给主线程发送消息?就差一点就成功啦,来人帮我看看
- android检测wifi和3g状态失败,下面是代码,求大神帮忙看下!
- android截取图片一个圆,得到的图片总是存在锯齿,怎么办?
- AlertDialog的使用和自定义Dialog
- android蓝牙开发 连接失败时如何判断是何种原因导致的失败?
- 有什么办法可以更改 或者模拟手机的 IMEI 和 IMSI
- cocos2dx加入box2d后打包APK提示file not found该怎么办啊
- Android自定义相机startPreview在部分机型failed
- android 身份证拍照后进行图片识别,抓取身份证信息
- 求安卓OTG链接尼康相机获取照片解决方案
- 关于 Location Controls不能设置的问题
- onCreateOptionsMenu问题
http://ouchxp.iteye.com/blog/693344下载一个Eval.jar也能实现。