Setting working directory for ProcessBuilder is not working - java

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.

Related

Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified with AWS

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

Java exec() function arguments

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

IOException when starting a process

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)

ffmpeg running on windows with java

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

How to invoke multiple java programs in separate command window from single class using Runtime.exex() in Windows OS?

I am using Runtime.exex() something like this to invoke 2 different java programs in 2 separate command windows (in windows 7 environment).
public class Invoke{
public void main(...){
String[] class1 = {"start", "java", "A"}; //Assume A.java is already compiled
String[] class2 = {"start", "java", "B"}; //Assume B.java is already compiled
try{
Runtime.getRuntime().exec(class1);
Runtime.getRuntime().exec(class2);
}catch(Exception e){
e.printStackTrace();
}
}
}
But its giving me Exception
java.io.IOException: Cannot run program "start": CreateProcess error=2, The syst
em 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 initialConfig.StartApp.win_startProg(StartApp.java:95)
at initialConfig.StartApp.main(StartApp.java:134)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find th
e file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
Do I need to create a separate .bat file to invoke these two programs and then use these bats here in Runtime.exec()? Please Help. Thanks.
I think start is an internal command of cmd.exe. So try:
String[] class1 = {"cmd.exe", "/c", "start", "java", "A"};
This leads to invoking java in separate process and window - and will not wait for it to terminate.

Categories