I am trying to run some Mercurial commands from a java program. I build my Process using a ProcessBuilder like this:
final ProcessBuilder procBuilder = new ProcessBuilder("hg", "log");
procBuilder.directory(new File("/Users/feuerball/workspace/www"));
final Process proc = procBuilder.start();
The folder www contains a Mercurial repository, hg is installed and in the systems PATH. But my program throws an exception when I start the process. This is the stacktrace:
Exception in thread "main" java.io.IOException: Cannot run program "hg" (in directory "/Users/feuerball/workspace/www"): error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1042)
at de.feuerball.tests.Test.main(Test.java:16)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:185)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1023)
... 1 more
Why do I get this error?
Update
To show that the directory does exist, I have changed the code a little bit:
final File repo = new File("/Users/feuerball/workspace/www");
System.out.println("Directory? " + repo.isDirectory());
System.out.println("Readable? " + repo.canRead());
System.out.println("Writable? " + repo.canWrite());
final ProcessBuilder procBuilder = new ProcessBuilder("hg", "log");
procBuilder.directory(repo);
final Process proc = procBuilder.start();
Now here is the proof:
Directory? true
Readable? true
Writable? true
Exception in thread "main" java.io.IOException: Cannot run program "hg" (in directory "/Users/feuerball/workspace/www"): error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1042)
at de.brushmate.tests.Test.main(Test.java:22)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:185)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1023)
... 1 more
ProcessBuilder doesn't use the env variable PATH, it can't find "hg", you need to specify the absolute path to "hg" (like /usr/bin/hg if you're using Linux)
Related
The script I'm trying to run is 'root/tufimail'. The code below produces this error
Exception in thread "main" java.io.IOException: Cannot run program "root/tufimail": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at java.lang.Runtime.exec(Runtime.java:450)
at java.lang.Runtime.exec(Runtime.java:347)
at rss.RSSFeedParser.main(RSSFeedParser.java:450)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
Is there something wrong with my syntax? Why is it not finding the file?
Runtime r= Runtime.getRuntime();
Process exec = r.exec("/root/tufimail \"<person#aplace.com; person2#aplace.com>\" \"Tesing 2 recipients\" \"I love booty\"");
try {
exec.waitFor();
System.out.println(exec.getOutputStream());
} catch (InterruptedException ex) {
Logger.getLogger(RSSFeedParser.class.getName()).log(Level.SEVERE,null, ex);
}
Here are the file properties:
Here is the linux directory :/root/tufimail
[root#abox ~]# ls -l tufimail
-rwxr-xr-x 1 root root 595 Aug 20 14:35 tufimail
Getting issue Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified while executing java program from intellij but when executing same command from cmd prompt then it's working properly.
public class TestRunProgram {
public static void main(String[] args) throws IOException {
String commandRun = "aws apigateway get-api-keys --profile awsProdUser --region eu-west-1";
System.out.println(commandRun);
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(commandRun);
}}
Getting below issue after execution
java.io.IOException: Cannot run program "aws": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at java.lang.Runtime.exec(Runtime.java:450)
at java.lang.Runtime.exec(Runtime.java:347)
I have found a solution after installing AWS CLI for windows. Initially, I was installing it from python.
https://docs.aws.amazon.com/cli/latest/userguide/install-windows.html
I am trying to run a file called test.pdf which is located at C:/Software/ using ProcessBuilder. Following is my code
public static void main(String[] args) throws IOException {
ProcessBuilder pb = new ProcessBuilder("test.pdf");
pb.directory(new File("C:/Software/"));
pb.start();
}
And I'm getting the following exception.
Exception in thread "main" java.io.IOException: Cannot run program "test.pdf" (in directory "C:\Software"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at com.test.Test.main(Test.java:12)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 2 more
I checked this How to set working directory with ProcessBuilder thread in stackoverflow. But didn't have any luck. Can anyone help on this? Thanks
Use below code :
String fileToOpen = "test.pdf";
List<String> command = new ArrayList<String>();
command.add("rundll32.exe");
command.add("url.dll,FileProtocolHandler");
command.add(fileToOpen);
ProcessBuilder builder = new ProcessBuilder();
builder.directory(new File("C://Software//"));
builder.command(command);
builder.start();
It will open your pdf .
Just change the file name if you want to open other file in same directory.
What is the difference between this-
Process process=Runtime.getRuntime().exec(
new String[] {"cmd", "/c", command.toString()},
null, new File("D:/test"));
and this-
Process process=Runtime.getRuntime().exec(
command.toString(), null, new File("D:/test"));
If I use the first one it's working but if I use the second one it's giving below exception-
java.io.IOException: Cannot run program "gst" (in directory "D:\test"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
at java.lang.Runtime.exec(Runtime.java:617)
at java.lang.Runtime.exec(Runtime.java:450)
at CommandInvoker.main(CommandInvoker.java:38)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:385)
at java.lang.ProcessImpl.start(ProcessImpl.java:136)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022)
... 3 more
I have simple test class as
public static void main(String[] args) throws IOException {
String[] line = {"ffmpeg -i D:\\hadoop-video\\testVideo\\xyz.mp4 %d.png"};
Runtime.getRuntime().exec(line);
}
when I try to run this I am getting
Exception in thread "main" java.io.IOException: Cannot run program "ffmpeg -i D:/hadoop-video/testVideo/xyz.mp4 %d.png": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at ImageTest.main(ImageTest.java:13)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
however file is present on my windows7 machine with location
D:\hadoop-video\testVideo\xyz.mp4
i tried removing .mp4 and then run also not working . please suggest what might be wrong
Where ffmpeg.exe is installed? try to use full path to execute the ffmpeg.exe
e.g.
D:\ffmpeg\bin\ffmpeg.exe
then
String cmd[] = {"D:\\ffmpeg\\bin\\ffmpeg","-i","D:\\ffmpeg\\hadoop.mp4","D:\\ffmpeg\\img%d.png"};
Runtime.getRuntime().exec(cmd);
or
Process process = new ProcessBuilder(Arrays.asList(cmd)).start();