Is There an Eval() Function in Java

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 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.

eval() function in java script throwing error with direct value

That has nothing to do with eval.

The error is produced by 99.toString. The reason is that 99. is read as a number (equivalent to 99.0) and then toString is just a random word that doesn't fit the syntax:

99.0 toString  // what the parser sees

To fix it, you need to keep . from being treated as part of the number. For example:

99 .toString()    // numbers can't contain spaces, so '99' and '.' are read separately
(99).toString() // the ')' token prevents '.' from being read as part of the number
99.0.toString() // '99.0' is read as a number, then '.toString' is the property access
99..toString() // same as above, just with '99.' as the number
99['toString']() // using [ ] for property access, no '.' at all

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.

How to evaluate a math expression given in string form?

With JDK1.6, you can use the built-in Javascript engine.

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;

public class Test {
public static void main(String[] args) throws ScriptException {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String foo = "40+2";
System.out.println(engine.eval(foo));
}
}

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.



Related Topics



Leave a reply



Submit