开发者

Is there a java equivalent of the python eval function?

开发者 https://www.devze.com 2023-03-30 05:37 出处:网络
Is there a java equivalent of the python eval function? This would be a function which takes an arbitrary string and attempts to execute it i开发者_如何学Gon the current context.Based on this Java Ti

Is there a java equivalent of the python eval function?

This would be a function which takes an arbitrary string and attempts to execute it i开发者_如何学Gon the current context.


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.


"Yes" and "no". Yes in that it's possible. No in that it's not standard and has a number of limitations.

See BeanShell which allows execution of limited Java from a process that is, well, Java. I have never tried to use it as a library and can not vouch for its use as such.

BeanShell is a small, free, embeddable Java source interpreter with object scripting language features, written in Java. BeanShell dynamically executes standard Java syntax and extends it with common scripting conveniences such as loose types, commands, and method closures like those in Perl and JavaScript.

It is however, far more restrictive/limited than say eval in Python or another dynamic language. (Consider JRuby, Jython, Groovy and Clojure as some dynamic counterparts that run on the JVM). The local Java variable names in the surrounding code are all compiled away and thus not accessible, for instance.

I would recommend rethinking the approach, if possible ;-)

Happy coding.


If you have access to groovy, you could always use Eval.me(String expression) [api]. This will execute your Java (really groovy) code in the current context.


It's not that simple in Java. Java is a compiled language and in order to "eval" some code, it needs to be compiled. Python is interpreted and thus "eval" is much simpler.


The Java Scripting API is probably the closest to a standard way to execute strings as code in the JVM. JavaScript is supported in the Sun JVM out of the box, but isn't guaranteed to be supported by all JVM implementations.


It may be that you wish to execute a DOS or unix command in the workflow of Java program execution...

String command = "cmd /c dir /s";
String homeDir = "C:\\WINDOWS";
Process process = Runtime.getRuntime().exec(command + " " + homeDir);

And may be you wish to process the output...

final InputStream in = process.getInputStream();
int ch;
while((ch = in.read()) != -1) {
   System.out.print((char)ch);
}

Close code with try, catch

0

精彩评论

暂无评论...
验证码 换一张
取 消