16

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 in the current context.

6
  • 1
    No. Java is all pre-compiled, so the context does not even exist at runtime. Commented Aug 22, 2011 at 5:02
  • 1
    Is there anything you are trying to achieve? Sometimes it can be easier if you give us more context. Commented Aug 22, 2011 at 5:03
  • 1
    Is java-tips.org/java-se-tips/java.lang/… of any use to you? Perhaps you could wrap up this big example into a simple function. It uses a non-standard class though. Commented Aug 22, 2011 at 5:09
  • @Ray: Based on the other answers, I think your comment should be an answer too. Commented Aug 22, 2011 at 5:12
  • Gabe, you know since I never had any classroom instruction in computer science, I never understood that a compiled language wouldn't have a context. But sometimes the concept of a context is still used (like an Android application context) but I guess then it just refers to a sort of "parent object" and not a real context. Is this also the reason that compiled languages are so much faster, because they don't need to maintain a context? Commented Aug 22, 2011 at 12:10

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

2 Comments

+1 for BeanShell. I wouldn't recommend using it to accomplish functional (core) requirements functionality in a system, but I've used it on numerous occasions in web applications as a tool for runtime diagnostics.
pst I think you're right, doing this type of stuff won't make the problem I have any easier.
1

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.

Comments

0

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.

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.