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

        //afaik the 3 lines above are not necessary. But just to be sure..

    } catch ( IOexc) {


Program output

true 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 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

2 answers


new ProcessBuilder(Dir+ExeName, 
     "-monitor=\"PnP-Monitor (Standard)\"", 


This will solve two things:

  • You are using the fully qualified name for the executable.
  • Yours detachArgument

    seems to be actually two arguments.


I'm sure this might be the problem File(Dir));

Returns for starters , so you just redirect the output of the directory file?

Maybe try this;

ProcessBuilder pb = new ProcessBuilder(ExeName, DetachArgument); File(Dir));


in German for completeness

Im ziemlich sicher, dass dies das Problem sein könnte

 pb = (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

gibt einen so that Ihr gerade Umleiten der Ausgabe einer Datei des Verzeichnisses?

Vielleicht versuchen Sie dies;

 ProcessBuilder pb = new ProcessBuilder (EXEName, DetachArgument); (new File (dir));




All Articles