Link to home
Start Free TrialLog in
Avatar of Kevin B
Kevin B

asked on

How to execute a Python program and gather return output in Java

Hi, I have python code that accepts a command line argument and uses NLTK to determine the classification of that argument and the accuracy of fitting that classification.  It prints the results to the console for now.  I would like to call this python code in java and supply the command line arg and then gather the return output.  

I know I can use the following code to execute the python program

String[] command = {"/bin/bash", "-c", "python3 " + scriptLocation + " < " + scriptArgs.getAbsolutePath()};
            log().info(command[0] + " " + command[1] + " " + command[2]);
            ProcessBuilder builder = new ProcessBuilder(command);
            builder.redirectErrorStream();
            Process process = builder.start();
            process.waitFor();

Open in new window


However, I'm not sure how to get the printed results from the console into my java program.  Can anyone help me out?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:) I'm going to edit that link soon btw