How could I call Java code dynamically?

你离开我真会死。 提交于 2019-12-17 19:55:40

问题


How could I write Java code that is executed like javascript code used together with the eval function?

What I would like to achieve would be something like this:

System.execute ("String str = \"test\"; System.out.println(str);");

which would print the word 'test'. (10x dehmann )

A code sample would help a lot.


回答1:


Look into BeanShell or Groovy. Both will give you reasonable solutions--but those solutions rely on my interpretation of your problem, which may be flawed.




回答2:


I've used the JavaScript engine shipped with Java 6 and it works quite well. The performance of the engine is very very decent.

Check the page http://java.sun.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html




回答3:


I believe the compiler API is present in the current Java SE 6. See the javadoc.

See this blog post for details and an example.




回答4:


For completeness sake, the Compiler API is coming in the next version of Java..



来源:https://stackoverflow.com/questions/588242/how-could-i-call-java-code-dynamically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!