By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,680 Members | 1,868 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,680 IT Pros & Developers. It's quick & easy.

java processbuild CreateProcess error=2 for command "Dir"

P: 6
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();
Apr 7 '11 #1
Share this Question
Share on Google+
11 Replies


Dheeraj Joshi
Expert 100+
P: 1,123
May be the path you specified is wrong.

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

Regards
Dheeraj Joshi
Apr 7 '11 #2

P: 6
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:

Expand|Select|Wrap|Line Numbers
  1. package tests;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. public class DoProcessBuilder {
  7.   public static void main(String args[]) throws IOException {
  8.  
  9.     if (args.length <= 0) {
  10.       System.err.println("Need command to run");
  11.       System.exit(-1);
  12.     }
  13.  
  14.     Process process = new ProcessBuilder(args).start();
  15.     InputStream is = process.getInputStream();
  16.     InputStreamReader isr = new InputStreamReader(is);
  17.     BufferedReader br = new BufferedReader(isr);
  18.     String line;
  19.  
  20.     System.out.printf("Output of running %s is:", 
  21.        Arrays.toString(args));
  22.  
  23.     while ((line = br.readLine()) != null) {
  24.       System.out.println(line);
  25.     }
  26.  
  27.   }
  28.  }  
The arg pasted is "dir".
Apr 7 '11 #3

Dheeraj Joshi
Expert 100+
P: 1,123
args is an array. You can not pass whole array.
It must be something like
Expand|Select|Wrap|Line Numbers
  1. 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
Apr 7 '11 #4

Dheeraj Joshi
Expert 100+
P: 1,123
Take a look at the ProcessBuilder class here.

In particular look at the constructors .

Regards
Dheeraj Joshi
Apr 7 '11 #5

P: 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:

Expand|Select|Wrap|Line Numbers
  1. package tests;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. public class DoProcessBuilder {
  7.   public static void main(String args[]) throws IOException {
  8.  
  9.     Process process = new ProcessBuilder("dir").start();
  10.     InputStream is = process.getInputStream();
  11.     InputStreamReader isr = new InputStreamReader(is);
  12.     BufferedReader br = new BufferedReader(isr);
  13.     String line;
  14.  
  15.     System.out.printf("Output of running %s is:", 
  16.        Arrays.toString(args));
  17.  
  18.     while ((line = br.readLine()) != null) {
  19.       System.out.println(line);
  20.     }
  21.  
  22.   }
  23.  }
Apr 7 '11 #6

Dheeraj Joshi
Expert 100+
P: 1,123
is "dir" a process?
If you try to execute notepad, i am sure it will execute. Quick execute of following code executed notepad.

Expand|Select|Wrap|Line Numbers
  1. String args1 = "notepad";
  2. Process process = null;;
  3. try {
  4.     process = new ProcessBuilder(args1).start();
  5. } catch (IOException e) {
  6.     e.printStackTrace();
  7. }
  8.  
Similarly you must execute the process which you want.

Regards
Dheeraj Joshi
Apr 7 '11 #7

P: 6
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?
Apr 7 '11 #8

Dheeraj Joshi
Expert 100+
P: 1,123
Yes, you can invoke a separate Java process inside another.

Regards
Dheeraj Joshi
Apr 7 '11 #9

P: 6
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?
Apr 7 '11 #10

P: 6
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.
Apr 8 '11 #11

P: 1
@Donald Likens

what you understand please share with me, i am also trying to do that through java program but i got the error
Expand|Select|Wrap|Line Numbers
  1. "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
Jul 10 '12 #12

Post your reply

Sign in to post your reply or Sign up for a free account.