javacompiler - Janino 在 android 应用程序中创建一个 java 代码游乐场
问题描述
我正在尝试创建一个 java 学习 android 应用程序。它应该有一个供用户使用的代码游乐场;可以运行简单的java代码。我将用户代码保存在 java 代码中并尝试运行它。我正在使用 JANINO ,但问题是我无法加载包括用户代码在内的外部类。这是我的代码;
String UsersCodeInput;
public void Run() throws FileNotFoundException, IOException
{
File sourcepath = new File(Environment.getExternalStorageDirectory()+"/MainFolder");
File JavaFile = new File(sourcepath.getPath()+"A.java");
FileOutputStream fos = new FileOutputStream(JavaFile);
fos.write(UsersCodeInput.getBytes());
fos.close();
// now Users Code is Saved.
//trying to run it by means of janino :
ClassLoader cl = new JavaSourceClassLoader(
this.getClass().getClassLoader(), // parentClassLoader
new File[] {sourcepath}, //our sourceFolder
"UTF-8" //Encodeing
);
try
{
// problem is here. next line is not working well; throws classNotFound.
Object o = cl.loadClass(".A").newInstance();
//the class implements runnable.
((Runnable) o).run();
}
catch (ClassNotFoundException e)
{Toast.makeText(this,e.getCause().toString(),Toast.LENGTH_SHORT).show();}
catch (InstantiationException e)
{Toast.makeText(this,e.getCause().toString(),Toast.LENGTH_SHORT).show();}
catch (IllegalAccessException e)
{Toast.makeText(this,e.getCause().toString(),Toast.LENGTH_SHORT).show();}
}
这是 JANINOs 教程;阅读“源代码编译器”部分 http://janino-compiler.github.io/janino/
谢谢。
解决方案
推荐阅读
- python - 如何为 Python 数据类编写 Sphinx 文档?
- django - 自定义 Django All-auth 注册表单在注册后不发送确认电子邮件
- sqlite - 查询同一日期,不同年份 SQAlchemy
- python - socket.send() 使用线程和nest_asyncio时出现异常
- php - 以 Blob 格式保存图像(以 Blob 格式转换时出错)
- twilio - 有人知道如何使用 Twilio 录制和播放录音吗?
- java - 程序不打印矩阵中素数的正确总和值
- python - 获取用于在 tensorflow 中训练自定义模型的 imagenet 数据集的任何简单方法?
- c++ - 声明类数组但不可能
- java - 如何在 powershell 中使用 javac?