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.)
I found this in forums.oracle.com
Allows the reuse of a process to execute multiple commands in Windows: http://kr.forums.oracle.com/forums/thread.jspa?messageID=9250051
You need something like
SyncPipe Class:
Every execution of
exec
spawns a new process with its own environment. So your second invocation is not connected to the first in any way. It will just change its own working directory and then exit (i.e. it's effectively a no-op).If you want to compose requests, you'll need to do this within a single call to
exec
. Bash allows multiple commands to be specified on a single line if they're separated by semicolons; Windows CMD may allow the same, and if not there's always batch scripts.As Piotr says, if this example is actually what you're trying to achieve, you can perform the same thing much more efficiently, effectively and platform-safely with the following:
Writing to the out stream from the process is the wrong direction. 'out' in that case means from the process to you. Try getting/writing to the input stream for the process and reading from the output stream to see the results.
Each of your exec calls creates a process. You second and third calls do not run in the same shell process you create in the first one. Try putting all commands in a bat script and running it in one call:
rt.exec("cmd myfile.bat");
or similarIf you want to run several commands in the cmd shell then you can construct a single command like this:
This page explains more.
This because every
runtime.exec(..)
returns aProcess
class that should be used after the execution instead that invoking other commands by theRuntime
classIf you look at Process doc you will see that you can use
getInputStream()
getOutputStream()
on which you should work by sending the successive commands and retrieving the output..