java processbuild CreateProcess error=2 for command "Dir"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Donald Likens
    New Member
    • Apr 2011
    • 6

    java processbuild CreateProcess error=2 for command "Dir"

    What am I missing? My research has told me that the following message means the system can not find the specified command.

    Cannot run program "dir": CreateProcess error=2, The system cannot find the file specified

    But the system finds the command when I type it in a command prompt.

    My Java command follows:

    Process process = new ProcessBuilder( args).start();
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    May be the path you specified is wrong.

    If the exe is in c drive. Then typically, you will write it as
    Code:
    c:\temp.exe
    But this need to be written as
    Code:
    c:\\temp.exe
    With out the code it is hard to guess. Can you post the code?

    Regards
    Dheeraj Joshi

    Comment

    • Donald Likens
      New Member
      • Apr 2011
      • 6

      #3
      My code was not working (same error) so I copied an example from the internet (that looked good to me). Here is the example I copied:

      Code:
      package tests;
      
      import java.io.*;
      import java.util.*;
      
      public class DoProcessBuilder {
        public static void main(String args[]) throws IOException {
      
          if (args.length <= 0) {
            System.err.println("Need command to run");
            System.exit(-1);
          }
      
          Process process = new ProcessBuilder(args).start();
          InputStream is = process.getInputStream();
          InputStreamReader isr = new InputStreamReader(is);
          BufferedReader br = new BufferedReader(isr);
          String line;
      
          System.out.printf("Output of running %s is:", 
             Arrays.toString(args));
      
          while ((line = br.readLine()) != null) {
            System.out.println(line);
          }
      
        }
       }
      The arg pasted is "dir".
      Last edited by Dheeraj Joshi; Apr 7 '11, 12:46 PM. Reason: Added code tags. Please add code tags when you post code.

      Comment

      • Dheeraj Joshi
        Recognized Expert Top Contributor
        • Jul 2009
        • 1129

        #4
        args is an array. You can not pass whole array.
        It must be something like
        Code:
        Process process = new ProcessBuilder(args[0]).start();
        Can you traverse through args array and figure out in which index the command is present and use it.

        Regards
        Dheeraj Joshi

        Comment

        • Dheeraj Joshi
          Recognized Expert Top Contributor
          • Jul 2009
          • 1129

          #5
          Take a look at the ProcessBuilder class here.

          In particular look at the constructors .

          Regards
          Dheeraj Joshi

          Comment

          • Donald Likens
            New Member
            • Apr 2011
            • 6

            #6
            Thanks for responding! For clarity I have changed the source as is listed below. No matter what I do I get the same error message:

            Code:
            package tests;
            
            import java.io.*;
            import java.util.*;
            
            public class DoProcessBuilder {
              public static void main(String args[]) throws IOException {
            
                Process process = new ProcessBuilder("dir").start();
                InputStream is = process.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line;
            
                System.out.printf("Output of running %s is:", 
                   Arrays.toString(args));
            
                while ((line = br.readLine()) != null) {
                  System.out.println(line);
                }
            
              }
             }
            Last edited by Dheeraj Joshi; Apr 7 '11, 01:28 PM. Reason: Please use code tags when you post the code block

            Comment

            • Dheeraj Joshi
              Recognized Expert Top Contributor
              • Jul 2009
              • 1129

              #7
              is "dir" a process?
              If you try to execute notepad, i am sure it will execute. Quick execute of following code executed notepad.

              Code:
              String args1 = "notepad";
              Process process = null;;
              try {
              	process = new ProcessBuilder(args1).start();
              } catch (IOException e) {
              	e.printStackTrace();
              }
              Similarly you must execute the process which you want.

              Regards
              Dheeraj Joshi
              Last edited by Dheeraj Joshi; Apr 7 '11, 01:31 PM. Reason: Typo fixed.

              Comment

              • Donald Likens
                New Member
                • Apr 2011
                • 6

                #8
                Your right... Funny the example I copies did a "dir"... What I really want to do is invoke a Java program as a seperate process. If there a way to do this?

                Comment

                • Dheeraj Joshi
                  Recognized Expert Top Contributor
                  • Jul 2009
                  • 1129

                  #9
                  Yes, you can invoke a separate Java process inside another.

                  Regards
                  Dheeraj Joshi

                  Comment

                  • Donald Likens
                    New Member
                    • Apr 2011
                    • 6

                    #10
                    Maybe I should start from the beginning... I have a TCP/IP socket server that needs to send information to a Syslog Deamon (UDP Port 514). When this TCP/IP socket server attempts to open the UDP port it gets a bind failure. I was led to believe that a TCP/IP Socket Server could not open a UDP port because they conflict with each other. My planned solution is to write the information to a temporary file and spin off another java program (running in a different process ID) to send the information in the temp file to the SYSLOG Deamon.

                    Does anyone have a way to do this?

                    Comment

                    • Donald Likens
                      New Member
                      • Apr 2011
                      • 6

                      #11
                      I have figured out how to issue the Java command...

                      p = new ProcessBuilder( "cmd", CMD).start();

                      cmd is a process CMD = "Java ... "

                      Thanks for all your help.

                      Comment

                      • pavan7678
                        New Member
                        • Jul 2012
                        • 1

                        #12
                        Originally posted by Donald Likens
                        I have figured out how to issue the Java command...

                        p = new ProcessBuilder( "cmd", CMD).start();

                        cmd is a process CMD = "Java ... "

                        Thanks for all your help.

                        what you understand please share with me, i am also trying to do that through java program but i got the error
                        Code:
                        "start cmd /c": CreateProcess error=2, The system cannot find the file specified
                        why this error comes what is reason behind it and how to resolve it

                        Comment

                        Working...