Java exec can't run program, error = 2 - java

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

Related

Running an external program from Katalon's Groovy / Java test case

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()

Issue while calling Python(Anaconda) from Java

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.

Running Maven Command From Java Program (From Eclipse) Mac OS

I want to run Maven command from Java in Mac but I am getting the following error:
Exception in thread "main" java.io.IOException: Cannot run program "mvn": error=2, No such file or directory
My code looks like this:
p = Runtime.getRuntime().exec("mvn -version");
If I run the following command, it works fine. The only issue is related to Maven:
p = Runtime.getRuntime().exec("ls -ltr");
check to see if you have <mvn_installation_location>/bin in your /etc/paths.

How to execute my own commands on terminal from java File

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";

Java Runtime.exec(COMMAND) doesnt work in win2K8 R2

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.

Categories

Resources