You should be able to use Process:
Process p = Runtime.getRuntime().exec(command);
command is a string you pass.
What's the error you're recieving?
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
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).
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
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):
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).
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
its giving error:2.
Can any one help on this
step 1: formulate a question
step 2: start a new thread rather than reviving one that has been dormant (not to say dead) for over years
step 3: give a wee bit more information about what you're doing...
whát are you trying? whát is giving error:2
...
hope this can help you get started.
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433