| | |
Executing dos commands in Java
![]() |
•
•
Join Date: Jan 2006
Posts: 24
Reputation:
Solved Threads: 0
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");
}
}
-------------------------------------------------
-------------------------------------------------------------------------
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");
}
}
-------------------------------------------------
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).
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).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
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):
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).
), 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)
String[] command = new String[4]; command[0] = "cmd"; command[1] = "/C"; command[2] = "dir"; command[3] = "c:\\"; Process p = Runtime.getRuntime().exec(command); BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); // read the output from the command String s = null; System.out.println("Here is the standard output of the command:\n"); while ((s = stdInput.readLine()) != null) { System.out.println(s); } // read any errors from the attempted command System.out.println("Here is the standard error of the command (if any):\n"); while ((s = stdError.readLine()) != null) { System.out.println(s); }
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).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
•
•
Join Date: Nov 2009
Posts: 1
Reputation:
Solved Threads: 0
0
#9 12 Days Ago
import java.io.*;
public class Dos
{
public static void main(String[] args)
{
try
{
String[] command = new String[4];
command[0] = "cmd";
command[1] = "/C";
command[2] = "del";
command[3] = "d:\\a.txt";
Process p = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
String s = null;
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
System.out.println("I am In try");
}
catch(Exception e){
System.out.println("I am In catch");
}
}
}
public class Dos
{
public static void main(String[] args)
{
try
{
String[] command = new String[4];
command[0] = "cmd";
command[1] = "/C";
command[2] = "del";
command[3] = "d:\\a.txt";
Process p = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
String s = null;
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
System.out.println("I am In try");
}
catch(Exception e){
System.out.println("I am In catch");
}
}
}
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: do while loop?
- Next Thread: Gap Sort
| Thread Tools | Search this Thread |
6 @param actuate android api applet application arc array arrays automation balls binary bluetooth bold business byte c++ chat class client code codesnippet collections compare component coordinates database defaultmethod detection doctype dragging ebook eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework hql html ide ideas image ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list map method methods mobile mysql netbeans newbie nextline parameter php pong problem program programming project recursion recursive scanner sell server set sms sort sql string sun swing swt terminal threads tree web websites windows






