4

In python, you can use exec() to execute a piece of code.

Is there's any way to do this in Java, so that I can call a function from a string, but also do other things as-well like getting some user input and running it as Java code.

Note: This is not the same as Is there an eval() function in Java?; That only does eval() and I want exec().

Community
  • 1
  • 1
ZacG
  • 97
  • 9
  • You might find the answer here: http://stackoverflow.com/questions/3716991/what-is-the-java-equivalent-of-pythons-subprocess-popen – Ray Toal Mar 13 '16 at 18:18
  • @ThorbjørnRavnAndersen, Andrew, Ray - this isn't about process execution... – Oliver Charlesworth Mar 13 '16 at 18:19
  • Use a `ProcessBuilder` – fge Mar 13 '16 at 18:19
  • Just to clarify, I am **not** trying to execute a terminal/cmd command. What the python `exec()` function does is executes a piece of python code. – ZacG Mar 13 '16 at 18:20
  • 2
    People seem to be confused what `exec()` does in Python. Short summary: It takes a string with Python source code, compiles it and executes it within the running interpreter. – Sven Marnach Mar 13 '16 at 18:22
  • 2
    No easy way. There's the [javax.tools](http://docs.oracle.com/javase/8/docs/api/javax/tools/package-summary.html) package, but it's a mess and it requires JDK (a real compiler) to be installed in order to work. If you just need to execute *some* code in your program, I suggest [javax.script](http://docs.oracle.com/javase/8/docs/api/javax/script/package-summary.html), which allows JavaScript execution. – Sergei Tachenov Mar 13 '16 at 18:24
  • 1
    Did your try search SO first? Looks like a duplicate of many: http://stackoverflow.com/questions/6051884/generating-compiling-and-using-java-code-at-run-time http://stackoverflow.com/questions/2946338/how-do-i-programmatically-compile-and-instantiate-a-java-class http://stackoverflow.com/questions/1064259/how-can-i-compile-and-deploy-a-java-class-at-runtime – fukanchik Apr 27 '16 at 13:32
  • [Reflection](https://docs.oracle.com/javase/tutorial/reflect/) is your friend here :) – ManoDestra May 05 '16 at 13:07

1 Answers1

6

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.

Community
  • 1
  • 1
Troncador
  • 3,356
  • 3
  • 23
  • 40