0

I would like to know how to enter commands into the cmd.exe (command prompt window) , after its been opened?

I have the code below to open cmd.exe:

Runtime rt= Runtime.getRuntime(); 
Process process= rt.exec("cmd.exe /c start cd c:\\ExecutionSDKTest_10.2.2");

But after it's been opened, I'd like to enter "ant compile" or any line, how do I do that??

Alex K.
  • 171,639
  • 30
  • 264
  • 288
jerryh91
  • 1,777
  • 10
  • 46
  • 77
  • Quick solution, you should be able to `"cmd.exe /c cd c:\\ExecutionSDKTest_10.2.2 && ant compile"` – Alex K. Jun 25 '12 at 14:23
  • This did not work: it simply cd to ExecutionSDKTest_10.2.2 Is there a more methodical way of entering multiple lines of codes in cmd.exe right after one another?? – jerryh91 Jun 25 '12 at 14:29
  • Try "cmd.exe /c cd c:\\ExecutionSDKTest_10.2.2 & ant compile" – pushy Jun 25 '12 at 14:39
  • This didnt work pushy, it still just cd to Execu... and didnt run ant compile – jerryh91 Jun 25 '12 at 14:54

4 Answers4

2

The normal way to do this would be to put the commands in a script and execute the script.

You will need to consume the output of the child process (stdout and stderr) on separate threads, or your process will block.

ewan.chalmers
  • 16,145
  • 43
  • 60
1

you dont need to open the command line to compile a program with a running program, check this out how to compile & run java program in another java program?

Community
  • 1
  • 1
JCarlos
  • 51
  • 1
  • 8
  • Hey @JCarlos, thanks for the response, but my goal is to run cmd commands from java, that execute other non-java tests/programs. Is there a methodical way of entering multiple lines of commands in cmd.exe with java language? – jerryh91 Jun 25 '12 at 14:30
  • maybe this will help http://www.ensta-paristech.fr/~diam/java/online/io/javazine.html – JCarlos Jun 25 '12 at 14:37
1

Not exactly the answer to you question. But you can use ProcessBuilder to set your process current directory (so you don't need to call "cd ..." anymore)

dragn
  • 1,030
  • 1
  • 8
  • 21
1

Try to pass a List to the ProcessBuilder

  final List<String> l = new ArrayList<String>();
    final String cmd = "C:/Program Files/Java/jre6/bin/";
    l.add("C:\\WINNT\\system32\\cmd.exe ");
    l.add("cd " + cmd);
    l.add("dir");
    l.add("java.exe -version");
cl-r
  • 1,264
  • 1
  • 12
  • 26
  • huh? the list that ProcessBuilder accepts in constructor is a list with a single program command and arguments to pass to it. You can't pass a numerous program calls to it. – dragn Jun 25 '12 at 14:41