944,093 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 49278
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 8th, 2006
1

Executing dos commands in Java

Expand Post »
Dear all,
Could you tell me how to go about executing MSDOS commands thru Java. I tried with the Process class but it gives an error:2.
Could anyone please advise.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Rajnesh is offline Offline
24 posts
since Jan 2006
Jan 8th, 2006
0

Re: Executing dos commands in Java

You should be able to use Process:

Process p = Runtime.getRuntime().exec(command);

command is a string you pass.


What's the error you're recieving?
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jan 8th, 2006
0

Re: Executing dos commands in Java

The error says:
-------------------------------------------------------------------------
Exception in thread "main" java.io.IOException: CreateProcess: start error=2
at java.lang.Win32Process.create(Native Method)
at java.lang.Win32Process.<init>(Unknown Source)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at Dos.main(Dos.java:5)
------------------------------------------------------------------
The program is:
---------------------------------------
import java.io.*;

public class Dos{
public static void main(String[] args) throws IOException{
Process p = Runtime.getRuntime().exec("start");
}
}
-------------------------------------------------
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Rajnesh is offline Offline
24 posts
since Jan 2006
Jan 9th, 2006
0

Re: Executing dos commands in Java

You will need to start a command interpreter in order to launch operating system level commands.
Under win32 that's done using the "cmd" command.
For example "cmd dir" would execute a dir command.

The entire system is rather tricky, I've never really gotten the hang of it (but then I've never tried to, preferring to keep my code operating system independent).
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jan 9th, 2006
0

Re: Executing dos commands in Java

Could you please explain the steps...of what you said because I tried and i think m not on the right track.....
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Rajnesh is offline Offline
24 posts
since Jan 2006
Jan 11th, 2006
0

Re: Executing dos commands in Java

Could you forwrd me tha whole prog as i did what you told, while no error is there, but no output is given
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Rajnesh is offline Offline
24 posts
since Jan 2006
Jan 12th, 2006
0

Re: Executing dos commands in Java

I've done a bit more R&D (you got me interested ), turns out it's trickier than it looks because you need to start a process and then start a process in that process and catch the output of that sub process.

As a minimum you'd end up with something like this (for a simple dir command):
Java Syntax (Toggle Plain Text)
  1. String[] command = new String[4];
  2. command[0] = "cmd";
  3. command[1] = "/C";
  4. command[2] = "dir";
  5. command[3] = "c:\\";
  6. Process p = Runtime.getRuntime().exec(command);
  7. BufferedReader stdInput = new BufferedReader(new
  8. InputStreamReader(p.getInputStream()));
  9.  
  10. BufferedReader stdError = new BufferedReader(new
  11. InputStreamReader(p.getErrorStream()));
  12.  
  13. // read the output from the command
  14.  
  15. String s = null;
  16. System.out.println("Here is the standard output of the command:\n");
  17. while ((s = stdInput.readLine()) != null) {
  18. System.out.println(s);
  19. }
  20.  
  21. // read any errors from the attempted command
  22.  
  23. System.out.println("Here is the standard error of the command (if any):\n");
  24. while ((s = stdError.readLine()) != null) {
  25. System.out.println(s);
  26. }

Forget the /C option (as I initially did) and it will only start a command shell and just sit there forever waiting for that to terminate (which it won't as there's nothing ever telling it to close).
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jan 15th, 2009
-3

Re: Executing dos commands in Java

Hi Friend,
Please find the sample code below.


Runtime r = Runtime.getRuntime();
Process p = r.exec("cmd");

BufferedReader in = new BufferedReader(new
InputStreamReader(p.getInputStream()));
String inputLine="" ;
while ((inputLine = in.readLine()) != null) {
Reputation Points: 8
Solved Threads: 0
Newbie Poster
aruntells is offline Offline
1 posts
since Jan 2009
Nov 16th, 2009
0
Re: Executing dos commands in Java
Java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2.  
  3. public class Dos
  4. {
  5. public static void main(String[] args)
  6. {
  7. try
  8. {
  9. String[] command = new String[4];
  10. command[0] = "cmd";
  11. command[1] = "/C";
  12. command[2] = "del";
  13. command[3] = "d:\\a.txt";
  14.  
  15. Process p = Runtime.getRuntime().exec(command);
  16.  
  17. BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
  18.  
  19. BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  20.  
  21. // read the output from the command
  22.  
  23. String s = null;
  24. System.out.println("Here is the standard output of the command:\n");
  25. while ((s = stdInput.readLine()) != null) {
  26. System.out.println(s);
  27. }
  28.  
  29. // read any errors from the attempted command
  30.  
  31. System.out.println("Here is the standard error of the command (if any):\n");
  32. while ((s = stdError.readLine()) != null) {
  33. System.out.println(s);
  34. }
  35. System.out.println("I am In try");
  36. }
  37.  
  38.  
  39. catch(Exception e){
  40. System.out.println("I am In catch");
  41. }
  42. }
  43. }
Last edited by Nick Evan; Feb 4th, 2010 at 3:42 am. Reason: Added code-tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
prianka is offline Offline
1 posts
since Nov 2009
Dec 13th, 2011
0
Re: Executing dos commands in Java
its giving error:2.
Can any one help on this
Reputation Points: 10
Solved Threads: 0
Newbie Poster
prathimat is offline Offline
1 posts
since Dec 2011
Message:
Previous Thread in Java Forum Timeline: Multiple Input/Output Streams in Java
Next Thread in Java Forum Timeline: Error with tika





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC