0
votes

I want to get windows command prompt instance in my java program so that batch commands like cls, dir etc. can be passed directly from my java program and would want to read its output just like we would normally pass this command manually after opening windows command prompt.

WindowsCmdInstance wcn = new WindowsCmdInstance();
CommandOp cop = new CommandOp();
cop = Cwcn.sendCommand("dir");
String readOutput = cop.readConsoleOutput();

I'm expecting something like the above pattern so that i can pass batch commands from my java program just like we manually would do from windows command prompt. I don't want to run my commands as a bat file by using java's runtime instance.

Please help me. Thanks in advance!!

Hussain

1

1 Answers

0
votes

You can run batch commands using Runtime.getRuntime().exec("cmd /C <command string>");. This will create a command prompt instance and run the commands. You can read the results back using the InputStream of the Process instance that is returned.

Not sure if you'll be able to run multiple commands and maintain the state of the command prompt the same way you would do at a standard command prompt: maybe you could just do Process cmd = Runtime.getRuntime().exec("cmd"); and then call cmd.getOutputStream().write("dir".getBytes()); or something like that.