Marjoram Marjoram - 2 years ago 122
Java Question

Trying to call python file from java

I am trying to run a python file from java.

I have tried

ProcessBuilder pb = new ProcessBuilder("python", "");
Process p = pb.start();

but that didn't work. No errors were given, but the file does not run.

I also tried


And got the same thing. No errors, but the file does not run.

Although it shouldn't matter, this is the .py file I am trying to run:

import winsound
Freq = 1500 # Set Frequency To 1500 Hertz
Dur = 500 # Set Duration To 1000 ms == 1 second
print "this file ran!!!"

Nothing is printed and no noise is heard. For various reasons, I am unable to use Jython, so please focus on this problem. Thanks!

Solution: As pointed out by Rahul Jha, my problem was that I had my python file in the incorrect folder. Thank you!!

Answer Source

Use BufferedReader to show your output.

class Test {
public static void main(String args[]) {
    try {
        Process p = Runtime.getRuntime().exec(
                "python ");
        BufferedReader in = new BufferedReader(new InputStreamReader(
    } catch (Exception e) {


and my python file is

print "Hello World Python.";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download