I am calling Python(Anaconda) code from Java using ProcessBuilder. It was working fine when I had installed only Python. But now I have removed Python and installed Anaconda. The code to call Python is not working now.
Getting the following error.
com.api.ai.helper.MLPythonAPI callPyhonScriptToPredict
SEVERE: null
java.io.IOException: Cannot run program "python": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at com.api.ai.helper.MLPythonAPI.callPyhonScriptToPredict(MLPythonAPI.java:66)
I am using python 3.6.1(anaconda3 4.4.0 64 bit) and following code to execute Python
ProcessBuilder pb = new ProcessBuilder().inheritIO().
command("python", "C:\\my_code\\test.py", userSays, filePath);
Process p = pb.start();
p.waitFor();
Can somebody help me in this.
I passed the complete Python path in command method(in stead of "python" and now it is working fine.
Related
I'm attempting to run a Python script using Java code in Intellij IDEA:
String commandString = "python /home/.../mypythonscript.py arg1 arg2";
try {
Process process = Runtime.getRuntime().exec(commandString);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
} catch(IOException e) {
System.out.println(e.getMessage());
The Python script outputs a single String, and works great when I run it from my Ubuntu Linux terminal. However, using the above Java to run the Python script returns an error message:
Failed to run Python Script: Cannot run program "python": error=2, No such file or directory.
The file path is correct (the path and arguments I give are identical to what I used to run the Python script in the terminal), and I've set the script permissions to 777. In Intellij, I've also gone to File>Settings>Appearance & Behavior>Path Variables and added a path to the folder that contains the Python script (it's being held in a folder for Pycharm projects). However, I'm still getting the error.
Any help would be much appreciated!
A more experienced Java developer friend of mine who doesn't have an account figured out the problem, the solution to which I'm posting here for anyone encountering a similar issue in the future:
The script was written in Python3, but the code I used:
String commandString = "python /home/.../mypythonscript.py arg1 arg2";
is for Python 2. It should have been written:
String commandString = "python3 /home/.../mypythonscript.py arg1 arg2";
In my Katalon project (running on Windoes OS), I like to run an external python program.
I was looking at several examples of how to execute an external program from a Java or Groovy program.
The problem is that no matter what program I try to run (the python script or even a simple cd command), I get the following error from Katalon-Studio:
[ERROR] - Test Cases/CallPython FAILED because (of) java.io.IOException: Cannot run program "cd": CreateProcess error=2, The system cannot find the file specified
Here are some of the options I tried:
Groovy:
println "python myp.py".execute().text
println "cd".execute().text
Java
Process p = Runtime.getRuntime().exec("python myp.py");
String[] cmd = ["python", "myp.py"];
Process p = Runtime.getRuntime().exec(cmd);
Process p = Runtime.getRuntime().exec("cd");
If everything has good HOME_PATH this code should help
ProcessBuilder pb = new ProcessBuilder("cmd", "python myp.py")
Process process = pb.start()
I am trying to make a eclipse project in Java to launch commands with some buttons. The libraries of Ros fuerte (These ones i want to use) are correctly installed and concretly i am trying to launch a ros command from a Java File using:
String cmd = "roscore";
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(cmd);
If i launch this command from a current terminal it works, but if i do it from the java file i have a problem because the terminal doesnt recognize the command.
java.io.IOException: Cannot run program "roscore": java.io.IOException: error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:475)
at java.lang.Runtime.exec(Runtime.java:610)
at java.lang.Runtime.exec(Runtime.java:448)
at java.lang.Runtime.exec(Runtime.java:345)
at LaunchTerminal.main(LaunchTerminal.java:24)
I think that i need to add some path or similar but i dont find the information. Does anybody know how to do it?
Thank u.
only normal commands are possible to execute like rm or cd ... al others must be referenced with full path of context
Do the following if you are using the groovy distribution:
String cmd = "source /opt/ros/groovy/setup.bash && roscore";
I've seen many similar posts but I am still stumped. I want to run this shell script with a Java program :
C:\\Users\\pro-services\\Desktop\\projects\\github\\cygwin\\TEST.sh
Here's what the code looks like:
ProcessBuilder pb = new ProcessBuilder("C:\\Users\\pro-services\\Desktop\\projects\\github\\cygwin\\TEST.sh");
Process p = pb.start();
I am trying to use Process Builder but I'm not sure how to get it to work - I keep getting the error:
Cannot run program "C:\Users\pro-services\Desktop\projects\github\cygwin\TEST.sh": CreateProcess error=193, %1 is not a valid Win32 application
I know there are other fields of ProcessBuilder that I'm not using. I also know that there may be syntax issues here. I wrote my script for Cygwin. Any help please?
Instead of passing the script as an executable to ProcessBuilder, pass in the path to bash binary and pass your script as argument:
ProcessBuilder pb = new ProcessBuilder("C:\\path\\to\\bash.exe", "C:\\Users\\pro-services\\Desktop\\projects\\github\\cygwin\\TEST.sh");
Process p = pb.start();
I am creating a process using the below 2 lines
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(COMMAND);
where COMMAND = "program.exe". program.exe is on the system's PATH variable.
Now the problem is that this does not work only in Windows 2K8 R2. It works fine on every other flavour of windows (winXP, win2003)
The error reported is :
java.io.IOException: Cannot run program "program.exe": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
You can run the program manually from the command line. You can run the program from within the parent process using the full path specification. I would say we can assume the program runs properly.
What else could be different? The environment the parent process is running under perhaps? I would check the PATH before launching the process. You are using Java. Too many Java programs use batch files to launch the JVM process. This batch file could be mucking with the PATH variable.
If this is your program and no batch file in involved I would check the current working directory when you run the parent program. It could affect what gets found at runtime as well.