Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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 seem to be very out of date. Another problem with the libraries is that, because this is a school assignment, 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 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.

share|improve this question
up vote 11 down vote accepted

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/

share|improve this answer

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.

share|improve this answer
    
sorry but have you used any jython here? all u have used are java std library functions? – KillBill Nov 26 '15 at 0:06
    
The ScriptEngine needs jython on the class path else 'engine' is null – Kevin Paton Jan 4 at 18:49

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.

share|improve this answer
    
You're right on both counts. I actually knew that Jython is still updated, I just didn't really clarify. On the second note, what I'm really trying to do is parse a string using regular expressions, however, the expression I'm using are not supported in Java. Yes, there are ways I could do this using only Java with simpler expressions, but that would mean more logic coding to parse the string, while this method (that is, the current regular expression I'm using) gives me pretty much exactly what I need. – 404 Not Found Apr 11 '12 at 2:10
    
Damn, well, it looks like even Python won't support the expressions I'm trying to use. I guess I'm going to have to rework it after all. – 404 Not Found Apr 11 '12 at 2:24
    
"more logic coding to parse the string" will be much less effort over all, no matter what two langauges you want to use together or how you're planning to do it. Trust me. Why don't you ask the question you're really wondering about - i.e. how to parse the string? Again, since this is a school assignment, they almost certainly don't mean for you to use advanced regex features. – Karl Knechtel Apr 11 '12 at 4:07
    
Well, the regex I was trying to use was to get matching parenthesis using balancing groups, but apparently it's only supported by .NET languages. In the end I just gave up trying to do nested (since it wasn't required), and just changed it to only single level. – 404 Not Found Apr 11 '12 at 4:42
2  
Balancing brackets is beyond the scope of what can normally sanely be called a regular expression, and is far beyond the scope of what is strictly called a regular expression. In fact, it is pretty much the canonical example of what you can't do with regular expressions. – Karl Knechtel Apr 11 '12 at 4:51

You can try using groovy. It runs on the JVM and it comes with great support for running external processes and extracting the output:

http://groovy.codehaus.org/Executing+External+Processes+From+Groovy

You can see in this code taken from the same link how groovy makes it easy to get the status of the process:

println "return code: ${ proc.exitValue()}"
println "stderr: ${proc.err.text}"
println "stdout: ${proc.in.text}" // *out* from the external program is *in* for groovy
share|improve this answer

First I would recommend to use ProcessBuilder ( since 1.5 )
Simple usage is described here
http://stackoverflow.com/a/14483787
For more complex example refer to
http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html

I've encountered problem when launching Python script from Java, script was producing too much output to standard out and everything went bad.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.