w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How to Run a Python Program that Takes Command-Line arguments from within a Java Program

Try capturing the standard output, so you can use print in python to output to this stream:

try {  
    Process p = Runtime.getRuntime().exec("python yourProgram.py thearg1
    InputStream stderr = proc.getOutputStream();
    InputStreamReader in = new InputStreamReader(stdout);
    BufferedReader reader = new BufferedReader(in);
    String line = null;
    while ((line = reader.readLine()) != null) {
        System.out.println("Python says: " + line);
    int exitVal = proc.waitFor();
} catch (Exception e) {  

In your python program, to output to the Java program, use print like:

print 'Hi!'

To see what the Java program sent you, you can use sys.argv:

print 'The Java program sent me: ', str(sys.argv)

Note: I am not a python expert, and have not done much with it. Please tell me if I get some syntax wrong.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.