Windows command from within my Java program.

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

Windows command from within my Java program.

 
0
  #1
Sep 23rd, 2009
Hi all,

I am trying to execute a windows command from within my java program, but I can't get it to execute the DOS command. Here's my code:
  1. try {
  2. Process p = Runtime.getRuntime().exec("cmd.exe /C cls");
  3. } catch (IOException e) {
  4. // catch exception
  5. }

But, the code doesn't clear the screen, nor it gives any error. Can you please let me know, what I am doing wrong here?

Thanks,
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,479
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Windows command from within my Java program.

 
0
  #2
Sep 23rd, 2009
What are you expecting it to do? That command isn't going to produce any noticeable output.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 26
Reputation: nomemory is an unknown quantity at this point 
Solved Threads: 5
nomemory nomemory is offline Offline
Light Poster

Re: Windows command from within my Java program.

 
0
  #3
Sep 23rd, 2009
Try to create the process using the ProcessBuilder class:
  1. ProcessBuilder pb= new ProcessBuilder(command);
  2. Process proc= pb.start();

"command" is a list of Strings. For Windows it should look like this:
  1. String[] command= { "cmd", "/C" , "dir" };

Additional:
After you create the output, you can obtain a stream of data from the process object itself:
  1. Scanner sc= new Scanner(proc.getInputStream());
(if you want to wrap the stream inside a scanner)
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC