Is There a Java Equivalent of the Python Eval Function

Is there a java equivalent of the python eval function?

Based on this Java Tip, compiling a Java string on the fly is indeed possible, if you are willing to use com.sun.tools.javac.Main.compile(source).

Classes in com.sun.tools are of course not part of the official Java API.

In Java 6 there is a Compiler API to provide programmatic access to the compiler. See the documentation for interface JavaCompiler.

No direct eval is provided by any standard API, but the tools exist to build one of your own. You might have "JVM inside of JVM" issues if you try and do a completely general eval, so it is best to limit the scope of what you want to do.

Also see: Is there an eval() function in Java? for some good commentary and explanations.

Is there an equivalent to Python's exec() function in Java?

You have a couple of options

1) Dynamically compile and load a java classes

How do you dynamically compile and load external java classes?

Like Peter Lawrey suggests using net.openhft.compiler you can do what you want

https://github.com/OpenHFT/Java-Runtime-Compiler

// dynamically you can call
String className = "mypackage.MyClass";
String javaCode = "package mypackage;\n" +
"public class MyClass implements Runnable {\n" +
" public void run() {\n" +
"System.out.println(\"Hello World\");\n" +
" }\n" +
"}\n";
Class aClass = CompilerUtils.CACHED_COMPILER.loadFromJava(className, javaCode);
Runnable runner = (Runnable) aClass.newInstance();
runner.run();

I tested this solution and it work perfectly.

Observation: You need to add in the dependencies of your project ${env.JAVA_HOME}/lib/tools.jar

2) Use another language inside of your Java that interpret your code, example with Jython:

String arbitraryPythonCode = "";
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec(arbitraryPythonCode);

You can also use Javascript, Groovy, Scala, etc.

Is there an eval() function in Java?

You can use the ScriptEngine class and evaluate it as a Javascript string.

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Object result = engine.eval("4*5");

There may be a better way, but this one works.

Is there an eval() function in Java?

You can use the ScriptEngine class and evaluate it as a Javascript string.

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Object result = engine.eval("4*5");

There may be a better way, but this one works.

Is there something like python's interactive REPL mode, but for Java?

edit
Since Java 9 there's JShell

Original answer follows

You can also use Groovy Console. It is an interactive console where you can do what you want. Since Groovy also includes classes from the core java platform, you'll be able to use those classes as well.

It looks like this:

Screenshot of Groovy

Why must exec (and not eval) be used for Python import statements?

The problem is that eval evaluates expressions and returns some result, while exec executes statements in some context. import is a statement, while re.match() is an expression.



Related Topics



Leave a reply



Submit