943,813 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 573
  • Java RSS
Sep 23rd, 2009
0

Windows command from within my Java program.

Expand Post »
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:
java Syntax (Toggle Plain Text)
  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,
Similar Threads
Reputation Points: 7
Solved Threads: 6
Junior Poster
new_2_java is offline Offline
127 posts
since Apr 2007
Sep 23rd, 2009
0

Re: Windows command from within my Java program.

What are you expecting it to do? That command isn't going to produce any noticeable output.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Sep 23rd, 2009
0

Re: Windows command from within my Java program.

Try to create the process using the ProcessBuilder class:
Java Syntax (Toggle Plain Text)
  1. ProcessBuilder pb= new ProcessBuilder(command);
  2. Process proc= pb.start();

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

Additional:
After you create the output, you can obtain a stream of data from the process object itself:
Java Syntax (Toggle Plain Text)
  1. Scanner sc= new Scanner(proc.getInputStream());
(if you want to wrap the stream inside a scanner)
Reputation Points: 19
Solved Threads: 6
Light Poster
nomemory is offline Offline
31 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java + grading program help
Next Thread in Java Forum Timeline: applet





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


Follow us on Twitter


© 2011 DaniWeb® LLC