如何解析存储在字符串中的Java代码?如何解析存储在字符串中的Java代码
例子:
String s = "int i = 2; System.out.println(\"the number is: \" + i);";
如何解析存储在字符串中的Java代码?如何解析存储在字符串中的Java代码
例子:
String s = "int i = 2; System.out.println(\"the number is: \" + i);";
使用Groovy!
String s = "int i = 2; System.out.println(\"the number is: \" + i);";
Binding binding = new Binding();
GroovyShell shell = new GroovyShell(binding);
Object value = shell.evaluate(s);
你可能会尝试使用JavaCompiler
接口...
搜索JavaCompiler
和ToolProvider
(这是您通常使用来获取接口的实例的保持)教程等。
另一种可能是Beanshell
究竟是什么你想实现什么? –
解析什么? – kan
你想执行字符串中的代码吗? –