Java.lang.ProcessBuilder throws unexpected IOException
maybe i'm a little naive, but i think it shouldn't behave like that. First my code:
private String ExeName="dc64cmd.exe";
private String Dir=System.getenv("ProgramFiles(x86)") +"\\12noon Display Changer\\";
private String DetachArgument = "-monitor=\"PnP-Monitor (Standard)\" -detach";
try {
System.out.println(new File(Dir+ExeName).exists());
//This prints "true" as expected.
ProcessBuilder pb = new ProcessBuilder(ExeName, DetachArgument);
pb=pb.directory(new File(Dir));
pb=pb.redirectError(ProcessBuilder.Redirect.INHERIT);
pb=pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb=pb.redirectInput(ProcessBuilder.Redirect.INHERIT);
//afaik the 3 lines above are not necessary. But just to be sure..
pb.start();
} catch (java.io.IOException IOexc) {
System.err.println(IOexc.toString());
}
Program output
true
java.io.IOException: Cannot run program "dc64cmd.exe" (in directory "C:\Program Files (x86)\12noon Display Changer"): CreateProcess error=2, Das System kann die angegebene Datei nicht finden
The last part is the German localized version "The system cannot find the file.
I don't understand why on the one hand the file exists, but on the other hand it cannot be found.
I dug a bit into java.lang.ProcessBuilder and found that the Exception was actually thrown in java.lang.ProcessImpl.java on line 189
handle = create(cmdstr, envblock, path,
stdHandles, redirectErrorStream);
This exception causes java.lang.ProcessImpl.Start (...) to run (starting at line 83) to close FileInputStreams and FileOutputStreams in the finally declaration for the try-statement. However, while debugging, I noticed that f2.close () (line 141) gets called twice, resulting in an exception being thrown, which is then interpreted as an unkown file.
My first guess would be a mistake, but I think my code is pretty trivial and shouldn't trigger an unconfirmed error.
It is likely that I made a stupid little mistake in my code in the first place ....
Hope you can read my bad english and my bad style, I am not used to writing about code ...
Any help is appreciated.
jdk 1.7.0_03
edit: It might be worth mentioning that the file I want to run is a 64-bit executable, although I installed it in an x86 folder.
source to share
I'm sure this might be the problem
pb=pb.directory(new File(Dir));
pb=pb.redirectError(ProcessBuilder.Redirect.INHERIT);
pb=pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb=pb.redirectInput(ProcessBuilder.Redirect.INHERIT);
pb.directory
Returns java.io.File for starters , so you just redirect the output of the directory file?
Maybe try this;
ProcessBuilder pb = new ProcessBuilder(ExeName, DetachArgument);
pb.directory(new File(Dir));
in German for completeness
Im ziemlich sicher, dass dies das Problem sein könnte
pb = pb.directory (new File (dir));
pb = pb.redirectError (ProcessBuilder.Redirect.INHERIT);
pb = pb.redirectOutput (ProcessBuilder.Redirect.INHERIT);
pb = pb.redirectInput (ProcessBuilder.Redirect.INHERIT);
Für einen Start pb.directory
gibt einen java.io.File so that Ihr gerade Umleiten der Ausgabe einer Datei des Verzeichnisses?
Vielleicht versuchen Sie dies;
ProcessBuilder pb = new ProcessBuilder (EXEName, DetachArgument);
pb.directory (new File (dir));
source to share