Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call and receive output from Python script in Java?

Tags:

java

python

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.

like image 935
404 Not Found Avatar asked Apr 10 '12 22:04

404 Not Found


People also ask

How do I call a Python script in Java?

First, we begin by setting up a ScriptContext which contains a StringWriter. This will be used to store the output from the script we want to invoke. We then use the getEngineByName method of the ScriptEngineManager class to look up and create a ScriptEngine for a given short name.

Can I call Python code from Java?

You can easily call python functions from Java code with Jython. That is as long as your python code itself runs under jython, i.e. doesn't use some c-extensions that aren't supported. If that works for you, it's certainly the simplest solution you can get. Otherwise you can use org.

Can Python communicate with Java?

From Java to PythonWe can also communicate in opposite direction: from Java app to Python app. To do that we just need to change reading to writing in Java app and writing to reading in Python app. Change in Java app is simple: we just write to standard output.


1 Answers

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 from this resource: http://www.devdaily.com/java/edu/pj/pj010016 import java.io.*;

public class JavaRunCommand {      public static void main(String args[]) {          String s = null;          try {                      // run the Unix "ps -ef" command             // using the Runtime exec method:             Process p = Runtime.getRuntime().exec("ps -ef");                          BufferedReader stdInput = new BufferedReader(new                   InputStreamReader(p.getInputStream()));              BufferedReader stdError = new BufferedReader(new                   InputStreamReader(p.getErrorStream()));              // read the output from the command             System.out.println("Here is the standard output of the command:\n");             while ((s = stdInput.readLine()) != null) {                 System.out.println(s);             }                          // read any errors from the attempted command             System.out.println("Here is the standard error of the command (if any):\n");             while ((s = stdError.readLine()) != null) {                 System.out.println(s);             }                          System.exit(0);         }         catch (IOException e) {             System.out.println("exception happened - here's what I know: ");             e.printStackTrace();             System.exit(-1);         }     } } 

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/

like image 139
Sevas Avatar answered Oct 01 '22 07:10

Sevas