| | |
Windows command from within my Java program.
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2007
Posts: 126
Reputation:
Solved Threads: 6
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:
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,
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)
try { Process p = Runtime.getRuntime().exec("cmd.exe /C cls"); } catch (IOException e) { // catch exception }
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,
•
•
Join Date: Sep 2009
Posts: 26
Reputation:
Solved Threads: 5
Try to create the process using the ProcessBuilder class:
"command" is a list of Strings. For Windows it should look like this:
Additional:
After you create the output, you can obtain a stream of data from the process object itself:
(if you want to wrap the stream inside a scanner)
Java Syntax (Toggle Plain Text)
ProcessBuilder pb= new ProcessBuilder(command); Process proc= pb.start();
"command" is a list of Strings. For Windows it should look like this:
Java Syntax (Toggle Plain Text)
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)
Scanner sc= new Scanner(proc.getInputStream());
![]() |
Similar Threads
- put the Dos 's command into java program? (Java)
- Running java command thru java program (Java)
- Run Unix Find command using java Program (Java)
- OS command from JAVA program... (Java)
Other Threads in the Java Forum
- Previous Thread: Java + grading program help
- Next Thread: applet
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application array arrays automation binary bluetooth button character chat class classes client code component consumer css csv database eclipse ee error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javaprojects jmf jni jpanel julia jvm key linked linux list loan loop map method methods mobile netbeans newbie objects oriented output panel phone print printf problem program programming project projects recursion replaydirector reporting researchinmotion robot rotatetext scanner screen se server service set size sms software sort sql string swing test threads transfer tree ubuntu windows






