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()
Related
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.
I'm trying to run Ant from a Java program. This works:
// Compile project
ProcessBuilder pb =
new ProcessBuilder("/usr/local/Cellar/ant/1.9.6/libexec/bin/ant", "-f", pathToProject + "build.xml");
Process p = pb.start();
p.waitFor();
However, this program will be used on machines other than my own, so I'd rather instantiate the ProcessBuilder like this:
new ProcessBuilder("ant", "-f", pathToProject + "build.xml");
Although ant -f build.xml works on my Mac terminal, placing the above code in my program outputs java.io.IOException: Cannot run program "ant": error=2, No such file or directory.
How can I programmatically show ProcessBuilder where Ant is located? This program will be run on OS X and possibly Ubuntu.
Note: This question is not Ant-specific. I need use ProcessBuilder to invoke other command-line tools as well.
Inject the absolute path of the ant to ProcessBuilder by making it customizable through properties file.This way you can run on multiple environments just by providing the corresponding path in the properties file.
I'm trying to run R.exe from a Java application with Java exec.
R is in the Environment Variables and if I execute it from cmd with "R" command it works.
If I execute an example instruction in Java and try to open notepad it works:
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("notepad");
But when I try to open R with this instruction:
Process p = rt.exec("R");
it gives me this kind of error:
java.io.IOException: Cannot run program "R": CreateProcess error=2
what could be? why cant I open R from Java?
You need to ensure that the Windows PATH includes the directory that R resides in for this to work
CreateProcess error=2 indicates the file can not be found
I am trying to run a simple ImageMagick command from a Java class and as I have to run only a few commands I thought instead of using Im4Java ,I could directly use ProcessBuilder.start().
I am using the following code-
ProcessBuilder pb = new ProcessBuilder("convert","pic2.png","pic52.png");
pb.directory(new File("/user/gaurav_kl"));
pb.start();
but I am getting the error
IOException - Cannot run program "convert" (in directory "/user/gaurav_kl"): error=2, No such file or directory
What could be the reason.
when I run the same command from terminal it works fine from any Dir as IM has been added to classpath
The behavior of ProcessBuilder when searching for a command executable is system/jvm dependent. While you might expect that it uses same logic as the underlying shell (i.e. BASH), there is no guarantee of it in the documentation.
Based on your experience (and others), it is better to provide the complete command path. For example:
String IMGK_PATH="/usr/local/magick/bin";
ProcessBuilder pb = new ProcessBuilder(IMGK_PATH + "/convert","pic2.png","pic52.png");
pb.directory(new File("/user/gaurav_kl"));
pb.start();
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();