可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
What's the easiest way to execute a Python script from Java, and receive the output of that script? I've looked for different libraries like Jepp or Jython, but most appear out of date. Another problem with the libraries is that I need to be able to easily include a library with the source code (though I don't need to source for the library itself) if I use a library.
Because of this, would the easiest/most effective way be to simply do something like call the script with runtime.exec, and then somehow capture printed output? Or, even though it would be very painful for me, I could also just have the Python script output to a temporary text file, then read the file in Java.
Note: the actual communication between Java and Python is not a requirement of the problem I am trying to solve. This is, however, the only way I can think of to easily perform what needs to be done.
回答1:
Not sure if I understand your question correctly, but provided that you can call the Python executable from the console and just want to capture its output in Java, you can use the exec()
method in the Java Runtime
class.
Process p = Runtime.getRuntime().exec("python yourapp.py");
You can read up on how to actually read the output here:
http://www.devdaily.com/java/edu/pj/pj010016
There is also an Apache library (the Apache exec project) that can help you with this. You can read more about it here:
http://www.devdaily.com/java/java-exec-processbuilder-process-1
http://commons.apache.org/exec/
回答2:
You can include the Jython library in your Java Project. You can download the source code from the Jython project itself.
Jython does offers support for JSR-223 which basically lets you run a Python script from Java.
You can use a ScriptContext
to configure where you want to send your output of the execution.
For instance, let's suppose you have the following Python script in a file named numbers.py
:
for i in range(1,10): print(i)
So, you can run it from Java as follows:
public static void main(String[] args) throws ScriptException, IOException { StringWriter writer = new StringWriter(); //ouput will be stored here ScriptEngineManager manager = new ScriptEngineManager(); ScriptContext context = new SimpleScriptContext(); context.setWriter(writer); //configures output redirection ScriptEngine engine = manager.getEngineByName("python"); engine.eval(new FileReader("numbers.py"), context); System.out.println(writer.toString()); }
And the output will be:
1 2 3 4 5 6 7 8 9
As long as your Python script is compatible with Python 2.5 you will not have any problems running this with Jython.
回答3:
I've looked for different libraries like Jepp or Jython, but most seem to be very out of date.
Jython is not "a library"; it's an implementation of the Python language on top of the Java Virtual Machine. It is definitely not out of date; the most recent release was Feb. 24 of this year. It implements Python 2.5, which means you will be missing a couple of more recent features, but it is honestly not much different from 2.7.
Note: the actual communication between Java and Python is not a requirement of the aforementioned assignment, so this isn't doing my homework for me. This is, however, the only way I can think of to easily perform what needs to be done.
This seems extremely unlikely for a school assignment. Please tell us more about what you're really trying to do. Usually, school assignments specify exactly what languages you'll be using for what, and I've never heard of one that involved more than one language at all. If it did, they'd tell you if you needed to set up this kind of communication, and how they intended you to do it.
回答4:
I met the same problem before, also read the answers here, but doesn't found any satisfy solution can balance the compatibility, performance and well format output, the Jython can't work with extend C packages and slower than CPython. So finally I decided to invent the wheel myself, it took my 5 nights, I hope it can help you too: jpserve(https://github.com/johnhuang-cn/jpserve).
JPserve provides a simple way to call Python and exchange the result by well format JSON, few performance loss. The following is the sample code.
At first, start jpserve on Python side
>>> from jpserve.jpserve import JPServe >>> serve = JPServe(("localhost", 8888)) >>> serve.start() INFO:JPServe:JPServe starting... INFO:JPServe:JPServe listening in localhost 8888
Then call Python from JAVA side:
PyServeContext.init("localhost", 8888); PyExecutor executor = PyServeContext.getExecutor(); script = "a = 2\n" + "b = 3\n" + "_result_ = a * b"; PyResult rs = executor.exec(script); System.out.println("Result: " + rs.getResult()); --- Result: 6
回答5:
Jep is anther option. It embeds CPython in Java through JNI.
import jep.Jep; //... try(Jep jep = new Jep(false)) { jep.eval("s = 'hello world'"); jep.eval("print(s)"); jep.eval("a = 1 + 2"); Long a = (Long) jep.getValue("a"); }
回答6: