ok I am trying to execute a python file (test1.py) with a java program. here's our code:
import java.io.*;
import java.nio.charset.StandardCharsets;
public class pro {
public Process mProcess;
public pro() {
Process process;
try {
process = Runtime.getRuntime().exec("test1.py");
mProcess = process;
} catch (Exception e) {
System.out.println("Exception Raised" e.toString());
}
InputStream stdout = (mProcess != null) ? mProcess.getInputStream(): null;
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout, StandardCharsets.UTF_8));
String line;
try {
while ((line = reader.readLine()) != null) {
System.out.println("stdout: " line);
}
} catch (IOException e) {
System.out.println("Exception in reading output" e.toString());
}
}
}
the line process = Runtime.getRuntime().exec(new String[] { "test1.py"});
throws Exception Raisedjava.io.IOException: Cannot run program "test1.py": CreateProcess error=193, %1 is not a valid Win32 application
whats wrong?
CodePudding user response:
The only problem I see is in this line
process = Runtime.getRuntime().exec("test1.py");
test1.py is just a python file, you need to tell exec how to run this file like we do in shell or command prompt
process = Runtime.getRuntime().exec("python test1.py");
CodePudding user response:
Runtime.getRuntime().exec()
runs something in the command line. So we changed it to process = Runtime.getRuntime().exec("python .\\test1.py")
and it works!