I am trying to execute command line arguments via Java. For example:
// Execute command
String command = "cmd /c start cmd.exe";
Process child = Runtime.getRuntime().exec(command);
// Get output stream to write from it
OutputStream out = child.getOutputStream();
out.write("cd C:/ /r/n".getBytes());
out.flush();
out.write("dir /r/n".getBytes());
out.close();
The above opens the command line but does not execute cd
or dir
. Any ideas? I am running Windows XP, JRE6.
(I have revised my question to be more specific. The following answers were helpful but do not answer my question.)
The code you posted starts three different processes each with it's own command. To open a command prompt and then run a command try the following (never tried it myself):
As i also faced the same problem and because some people here commented that the solution wasn't working for them, here's the link to the post where a working solution has been found.
https://stackoverflow.com/a/24406721/3751590
Also see the "Update" in the best answer for using Cygwin terminal
Try this link
You do not use "cd" to change the directory from which to run your commands. You need the full path of the executable you want to run.
Also, listing the contents of a directory is easier to do with the File/Directory classes