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";
Related
I am executing wkhtmltopdf command line tool from Java but it is throwing below error.
Cannot run program "wkhtmltopdf": error=2, No such file or directory
But note that when I execute this command line tool from my mac terminal then pdf got generated successfully. Please see below.
MacBook-Air-2:~ inDiscover$ wkhtmltopdf /var/folders/7y/2vr28n113p908ksnk0fnpqch0000gn/T/test7896850081571855407.html /Users/mymac/Documents/Project/emailbody/test2.pdf
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
I have seen lot many similar questions here (for ex: wkhtmltopdf: No such file or directory [ Closed ]) but issue with those questions are related to $PATH. In my case I believe that I have set the path to the executable to $PATH correctly. Please see below.
MacBook-Air-2:~ inDiscover$ locate wkhtmltopdf
/private/var/db/receipts/org.wkhtmltopdf.wkhtmltox.bom
/private/var/db/receipts/org.wkhtmltopdf.wkhtmltox.plist
/usr/local/bin/wkhtmltopdf
/usr/local/share/man/man1/wkhtmltopdf.1.gz
You can see here wkhtmltopdf has been added to $PATH (/usr/local/bin)
Also , see below the response for echo $PATH.
MacBook-Air-2:~ inDiscover$ echo $PATH
/Users/mymac/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
MacBook-Air-2:~ inDiscover$
I am getting this issue only when I try to execute the command from Java. Please see my java code below.
String VIEWPORT_SIZE = "2480x3508";
int CONVERSION_DPI = 300;
int IMAGE_QUALITY = 100;
List<String> cmd = new ArrayList<String>(Arrays.asList("wkhtmltopdf",
"--viewport-size", VIEWPORT_SIZE,
"--enable-local-file-access",
// "--disable-smart-shrinking",
"--dpi", String.valueOf(CONVERSION_DPI),
"--image-quality", String.valueOf(IMAGE_QUALITY)));
//cmd.addAll(extParams);
cmd.add("/var/folders/7y/2vr28n113p908ksnk0fnpqch0000gn/T/test7896850081571855407.html");
cmd.add("/Users/mymac/Documents/Project/emailbody/test3.pdf");
try {
ProcessBuilder pb = new ProcessBuilder(cmd);
if (Logger.level.compareTo(LogLevel.Info) >= 0) {
pb.inheritIO();
}
Process p = pb.start();
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
Am I really missing something?
Here are a few things you can check, one will hopefully resolve your issue.
Nearly all occasions I've seen where java is not finding an application that your shell or terminal does run are down to use of commands built into the shell / terminal (such as an alias), or because of PATH used in a script that you use to launch your java yourapp.ClassName ... is different to that set for the interactive shell / terminal.
The interactive shell / terminal may include or source from other scripts on startup - for example it may have run code in ~/.bashrc, ~/.login, ~/.profile, ... meaning that PATH declared inside interactive shell is not same as PATH presented to Java app when you launched it.
Hence you see terminal show PATH has /usr/local/bin:
MacBook-Air-2:~ inDiscover$ echo $PATH
/Users/mymac/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
But Java says no /usr/local/bin in PATH:
System.out.println("PATH="+System.getenv("PATH"));
/usr/bin:/usr/sbin:/sbin
So to fix your problem you could make java use absolute path to run the app wkhtmltopdf:
Arrays.asList("wkhtmltopdf" -> Arrays.asList("/usr/local/bin/wkhtmltopdf" ...
OR you could make java launch your shell / terminal so the terminal sources its env scripts as normal and runs the command:
# Confirm which shell your Terminal uses:
echo $SHELL
If you have say SHELL=/bin/bash you can run /bin/bash from java and let it work out where wkhtmltopdf is:
Arrays.asList("wkhtmltopdf" -> Arrays.asList("/bin/bash", "-c", "wkhtmltopdf" ...
OR if you have a script say runMyApp.sh to launch your java app, set the PATH before java yourclass.Name,
export PATH=/usr/local/bin:$PATH
java yourclass.Name
OR if you have a script say runMyApp.sh to launch your java app, make that it sources the same profile environment as your Terminal does. This depends on the SHELL but for some systems could be something like:
#!/bin/bash
# Load env from current user - "source" and "." may or may not work in the SHELL you are using:
source ~/.bashrc
# OR maybe other shell
. ~/.somefilerc
echo $PATH # Now hopefully includes same /usr/local/bin
java yourclass.Name
Try to add sudo in the front of wkhtmltopdf.
When I run:
new ProcessBuilder("kotlinc", "-help").start();
I get the error: Cannot run program "kotlinc": CreateProcess error=2, The system cannot find the file specified
I've tried:
If I check my path from windows, it contains C:\Apps\kotlinc\bin, and when I open explorer at that location, there's a file named kotlinc.
If I open cmd.exe anywhere, and run kotlinc, it works just fine.
If I print out my environment:
System.out.print(new ProcessBuilder("kotlinc", "-help").environment().get("Path"));
it contains C:\Apps\kotlinc\bin
If I run new ProcessBuilder("python3", "file.py").start(), it works just fine.
Rebooting my machine
Changing kotlinc to use the full file path is not an acceptable solution, as this is being run across multiple computers and platforms.
As far as I can tell, everything is setup correctly.
Why can't I run kotlinc from ProcessBuilder?
kotlinc is actually a batch file (kotlinc.bat), not a binary file. Therefore, you need to start it by executing the command cmd /c kotlinc.
I would like execute the following command with Java:
"C:\Program Files\MySQL\MySQL Server 5.5\bin\"mysqldump -u root
--password=xxxx --routines database > C:\Users\john\Desktop\backup.sql
The command works perfectly when I use the Windows cmd.exe but not with my Java application.
cmd = "\"C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\\"mysqldump
-u root --password=xxxx --routines database > C:\\Users\\john\\Desktop\\backup.sql
Runtime runtime = Runtime.getRuntime();
process = runtime.exec(cmd);
I got the following error:
"C:\Program Files\MySQL\MySQL Server 5.5\bin\"mysqldump -u root
--password=xxxx --routines database > C:\Users\john\Desktop\backup.sql java.io.IOException: Cannot run program ""C:\Program": CreateProcess
error=5, Access is denied
Do you have any idea?
Thank you.
You're trying to use a redirect (>) which is a function of the shell.
You don't have a shell, you're exec'ing the mysqldump process directly.
You either need to read the output stream from the process object and write it to a file yourself, or exec the windows shell to execute mysqldump for you along with the redirect.
Error 5 is either 1) cannot access the file/folder or 2) don't have permissions.
Looks like the space in your 'Program File' directory name may be causing trouble causing trouble. Check where it says "Cannot run program ""C:\Program". This is obviously not a reference to the command you are trying to run. What I would do is add MySQL to your path and then just call the mysqldump command without the path to it. If you don't want to do this, you can define a system variable and reference it with %MYSQL_HOME%\mysqldump.
Once you have the path resolved, if this still doesn't work, you'll want to look at the permissions for the command. You'll need to make sure that the process has permission to execute the command. I think this in local admin groups or something like that.
Dollars to donuts its your path tho.
I want to set a path in my Java program with this Windows command (this path contains some DLL files that are used in a native peripheral of my program):
c:\>path=%path%;"C:\Users\NetBeansProjects\IPTV1.7\3rd_party"
But this causes an exception when the program runs:
java.io.IOException: Cannot run program "path=%path%;C:\Users\NetBeansProjects\IPTV1.7\3rd_party\": CreateProcess error=2, The system cannot find the file specified
I don't know why I can set the path with no problem in the command prompt but then get an exception thrown in the code.
String path = "C:\\Users\\NetBeansProjects\\IPTV1.7\\3rd_party\\";
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("path=%path%;"+ path);
Your command
path=%path%;"C:\Users\NetBeansProjects\IPTV1.7\3rd_party"
is not a "real" windows command, but only a variable assignment, interpreted by your shell (cmd.exe), visible only in the same shell session and any commands (other programs) started from there.
When trying to execute this from Java with Runtime.exec(), the windows CreateProcess function tries to find a executable file with this strange name, which obviously does not exist (can't exist, I think), and you get this error.
Even if you could execute this, for example by calling cmd.exe, it would only influence this same cmd.exe process (and any programs started from there, not your own Java process (and programs started from here).
Depending on what you in fact want, you could, for example:
give the path with ProcessBuilder directly to the process which in fact needs it (like Aaron showed)
search by yourself for executable files, if you want this for finding commands in the next exec
put the variable assignment and other commands together in a .BAT or .CMD file and execute this.
When you type this on the command prompt, the cmd program processes it and changes the PATH variable for you. When you try this with Runtime, no cmd process is created and there is no command "path=%path%;C:\Users\NetBeansProjects\IPTV1.7\3rd_party\" on your harddisk (Windows actually tries to find a program with this exact name).
Put the commands in a .BAT or .CMD file. Windows automatically creates a cmd process to execute them for you.
You can use ProcessBuilder in java to spawn a process and control the environment that it gets. So you would use the ProcessBuilder environment method to set the PATH environment variable, then set the relevant command line, then launch. SO something like (untried):
ProcessBuilder b = new ProcessBuilder();
b.environment().put("PATH", whatever);
b.command(whatever);
Process p = b.start();
I am trying to run the following code from within Eclipse:
Process process = Runtime.getRuntime().exec("gs");
However I get the exception:
java.io.IOException: Cannot run
program "gs": error=2, No such file or
directory
Running gs from the command prompt (OS X) works fine from any directory as it is on my PATH. It seems eclipse doesn't know about my path environment variable, even though I have gone into run configurations and selected PATH on the environment tab.
In additional effort to debug this issue I tried the following code:
Process process = Runtime.getRuntime().exec("echo $PATH");
InputStream fromStdout = process.getInputStream();
byte[] byteArray = IOUtils.toByteArray(fromStdout);
System.out.println(new String(byteArray));
The output was $PATH, hmm. Can someone nudge me in the correct direction?
you are assuming that exec() uses a shell to execute your commands (echo $PATH is a shell command); for the sake of simplicity you can use System.getenv() to see your $PATH:
System.out.println(System.getenv("PATH"));
EDIT
Often a better and flexible alternative to Runtime.exec() is the ProcessBuilder class.
I had the same issue and i found the problem. The Path Variable in Eclipse had different content than the one from the command Line.
Solution:
Look up for the $Path variable in command Line and copy the content. Then open Run Configuration->Environment and select new. Name: $PATH Value: insert the copied content.
That solved the Problem.