| | |
Windows command from within my Java program.
![]() |
•
•
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 |
add android api applet application applications array arrays automation bank binary bluetooth chat class clear client code codesnippet collections component converter database development dice digit eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jni jpanel julia linux list loop looping main map method methods mobile myregfun mysql netbeans newbie nonstatic openjavafx parameter pearl php print problem program programming project recursion repositories scanner scrollbar server set size sms sort sorting spamblocker sql sqlserver state storm string superclass swing swt text-file thread threads tree windows






