Is there a Java equivalent Python Eval function?
Is there a Java equivalent Python Eval function?
This will be a function that uses an arbitrary string and attempts to execute it in the current context
Solution
Based on this Java tip, if you want to use COM sun. tools. javac. Main. Compile (source), you can compile Java strings on the fly
com. sun. The classes in tools are certainly not part of the official Java API
There is a compiler API in Java 6 to provide programmatic access to the compiler See the document for interface javacompiler
Any standard API does not provide direct Eval, but the existing tools build their own If you try to perform a completely generic Eval, you may have "JVM internal JVM" problems, so it's best to limit what you want to do
See also: is there an eval() function in Java? Some good comments and explanations