1 2 Previous Next 20 Replies Latest reply on Jan 16, 2007 7:29 PM by 843790

    Processbuilder ignores directory settings

    843790
      Hi!

      I recently came across this problem:
      ProcessBuilder pb = new ProcessBuilder( "my.exe" );
      pb.directory(new File("C:/Dokumente und Einstellungen/MyName/Desktop"));
      pb.start();
      The exe is there(!) and the path is correct!

      It produces:
      java.io.IOException: CreateProcess: my.exe error=2
      at java.lang.ProcessImpl.create(Native Method)
      at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
      at java.lang.ProcessImpl.start(ProcessImpl.java:30)
      at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)

      Error=2 is file not found.

      Beside the fact that the javadoc is wrong here
      see: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ProcessBuilder.html
      where it states pb.directory("myDir"). Which doesnt work since pb expects a File.

      Anyways using filemon i found out that my.exe is searched in my complete (!!!) windows path, which is driving me nuts because the user directory is ignored.

      The only way to get this going is using
       ... = ProcessBuilder(complete path with exe-name);
      ...start();
       
      Is there any reasonable work-around?

      BTW: I'm using Java 1.5.0_10
      BTW2: Neither adding the path to the builders environment (any of user.dir, path, ...) helps.

      Desperatly looking for help,
      BigOh
        • 1. Re: Processbuilder ignores directory settings
          843790
          I'm not sure you are interpretting the error message correctly. It looks to me like the directory containing my.exe is not in the PATH. I don't think it has anything to do with the working directory.
          • 2. Re: Processbuilder ignores directory settings
            843790
            Shouldnt he search the user dir for executables?

            Anyways if I removed the complete path from new ProcessBuilder(...) AND putting it in pb.environment() does not work. That's my problem.
            • 3. Re: Processbuilder ignores directory settings
              843790
              Shouldnt he search the user dir for executables?
              No! It searches the PATH! You could set the path dynamically using something like
                          ProcessBuilder pb = new ProcessBuilder("myexe.exe");
                          pb.directory( new File("Your working directory"));
                          Map<String,String> env = pb.environment();
                          env.put("PATH", "the directory containing you exe" + ":" + env.get("PATH"));
              >
              Anyways putting the path with pb's environment now
              works - but i still do not know what went wrong the
              last hours - i put the the path in every env-variable
              which existed but to no avail.
              But not the PATH or it would have worked.
              Probably a semicolon
              too much or so...
              Who knows!

              >
              Happy it now works. :)

              Thanks,
              BigOH
              • 4. Re: Processbuilder ignores directory settings
                843790
                It still does not work. I'm getting the same error again. Even with your snippet! The only way it works is to use
                new Processbuilder(complete path to exe)

                Puzzled,
                BigOh

                Message was edited by:
                BigOh
                • 5. Re: Processbuilder ignores directory settings
                  843790
                  Ok, here is a complete class to reproduce the error:
                  package pgk;
                  import java.io.*;
                  import java.util.*;
                  
                  public class Foo {
                      
                      public static void main(String [] args){
                          
                          ProcessBuilder pb = new ProcessBuilder("my.exe");
                          Map<String,String> env = pb.environment();
                          env.put("PATH", "C:/bar" + ":" + env.get("PATH"));
                          try{
                              pb.start();
                          }
                          catch(Exception e){
                              e.printStackTrace();
                          }
                          
                      }
                      
                  }
                  Giving:
                  java.io.IOException: CreateProcess: my.exe error=2
                  at java.lang.ProcessImpl.create(Native Method)
                  at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
                  at java.lang.ProcessImpl.start(ProcessImpl.java:30)
                  at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
                  at pgk.Foo.main(Foo.java:13)


                  c:\bar\my.exe exists...

                  :((
                  • 6. Re: Processbuilder ignores directory settings
                    843790
                    You need semicolon for Windows, I used the colon because I work with Linux.
                    env.put("PATH", "C:/bar" + ";" + env.get("PATH"));
                            
                    • 7. Re: Processbuilder ignores directory settings
                      843790
                      I thought so, but this doesn't solve it.
                      • 8. Re: Processbuilder ignores directory settings
                        843790
                        I thought so, but this doesn't solve it.
                        Post your current code! It works for me for an application I use on both Linux and Windows so without seeing the code it is difficult to say what is wrong.
                        • 9. Re: Processbuilder ignores directory settings
                          843790
                          I thought so, but this doesn't solve it.
                          Also, what is the latest error message.
                          • 10. Re: Processbuilder ignores directory settings
                            843790
                            Fresh out of my NetBeans:
                            package pkg;
                            import java.io.*;
                            import java.util.*;
                             
                            public class Foo {
                                
                                public static void main(String [] args){
                                    
                                    ProcessBuilder pb = new ProcessBuilder("my.exe");
                                    Map<String,String> env = pb.environment();
                                    env.put("Path", "C:/bar" + ";" + env.get("Path"));
                                    try{
                                        pb.start();
                                    }
                                    catch(Exception e){
                                        e.printStackTrace();
                                    }
                                    
                                }
                                
                            }
                            Error message:

                            init:
                            deps-jar:
                            Compiling 1 source file to C:\Java Projekte\build\classes
                            compile-single:
                            run-single:
                            java.io.IOException: CreateProcess: my.exe error=2
                            at java.lang.ProcessImpl.create(Native Method)
                            at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
                            at java.lang.ProcessImpl.start(ProcessImpl.java:30)
                            at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
                            at pkg.Foo.main(Foo.java:13)
                            • 11. Re: Processbuilder ignores directory settings
                              843790
                              Writing "Path" or "PATH" gives the same error.
                              • 12. Re: Processbuilder ignores directory settings
                                843790
                                Also changing the constructor to
                                ProcessBuilder pb = new ProcessBuilder("c:/bar/my.exe");
                                works without setting the environment. (Obviously)
                                • 13. Re: Processbuilder ignores directory settings
                                  843790
                                  This is more like I would expect to see.
                                  import java.io.*;
                                  import java.util.*;
                                  
                                  class AsyncPipe  extends Thread
                                  {
                                      public AsyncPipe(InputStream istrm, OutputStream ostrm)
                                      {
                                          istrm_ = istrm;
                                          ostrm_= ostrm;
                                      }
                                      
                                      public void run()
                                      {
                                          try
                                          {
                                              final byte[] buffer = new byte[1024];
                                              for (int length = 0; (length = istrm_.read(buffer)) != -1;)
                                              {
                                                  synchronized (ostrm_)
                                                  {
                                                      ostrm_.write(buffer, 0, length);
                                                  }
                                              }
                                          }
                                          catch (Exception e)
                                          {
                                              e.printStackTrace();
                                          }
                                      }
                                      
                                      private final OutputStream ostrm_;
                                      private final InputStream istrm_;
                                  }
                                  
                                  public class Foo
                                  {
                                      public static void main(String [] args) throws Exception
                                      {        
                                          ProcessBuilder pb = new ProcessBuilder("my.exe");
                                          pb.directory( new File(System.getProperty("C:/bar"))); // Or whatever directory you want for cwd
                                          Map<String,String> env = pb.environment();
                                          env.put("PATH", "C:/bar");
                                          
                                          Process p = pb.start();
                                          
                                          new AsyncPipe(p.getErrorStream(), System.err).start();
                                          new AsyncPipe(p.getInputStream(), System.out).start();
                                          
                                          final int returnCode = p.waitFor();
                                          System.out.println("Return code is " + returnCode);
                                      }
                                      
                                  }
                                  Unless your exe uses some sytem shared libraries you do not need to set the PATH to anything except the directory containing your exe.

                                  You can set the cwd (current working directory) to anything you feel like.

                                  You must handle stdout and stderr in a similar manner to my code. What you do with the output is up to you - I just print it out.

                                  I don't know how case sensitive the PATH variable name is but upper case is safe.

                                  You should wait until the process finishes (waitFor() ) before allowing your program to terminate.
                                  • 14. Re: Processbuilder ignores directory settings
                                    843790
                                    Sabre, I very very much appreciate the hard work you spend on this! I ran your code (had to change: new File(System.getProperty("C:/bar")) to new File("C:/bar") due to NullPointerException but sadly, sadly i does not run giving me exact the same error as before.

                                    BUT the origin of this bug seems to be NetBeans...

                                    If I set the path manually (using the control panel of windows) to include "C:\bar" while NetBeans runs, it does not work. If I restart NetBeans it DOES work (!). If I delete it from the path (NetBeans still running) it still works. Restarting NetBeans -> failure.

                                    So there seems to be a bug in NetBeans dealing with environment variables!
                                    (both 4.1 and 5.0 version)

                                    Thanks Sabre for helping me trace this down!
                                    BigOh
                                    1 2 Previous Next