5

Please see the code below

Runtime rt = Runtime.getRuntime();  
rt.exec("cmd /c start");
String[] cmd = {"LogParser", "Select top 10 * into c:\temp\test9.csv from application" };
rt.exec(cmd);

It opens the command window but the strings are not passed in after opening. Can someone tell me why this code won't place the strings into the command window?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

5 Answers5

2

The option /C means: Carries out the command specified by the string and then terminates.

So the other command is handled as a separated one.

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
1

Use OutputStreamWriter and write to the input stream of the process created.

Process p = Runtime.getRuntime().exec("cmd /K start") ;
Writer w = new java.io.OutputStreamWriter(p.getOutputStream());
w.append(yourCommandHere);

Also, the reason for using /K :

/K Run Command and then return to the CMD prompt.

Reference : http://ss64.com/nt/cmd.html

Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
  • ...and this will fail because `cmd` completes immediately after spawning a separate process. – Marko Topolnik Feb 22 '13 at 14:21
  • What will happen with your proposal is that `w` will append to `cmd`s standard input, instead of `LogParser`s -- which you don`t even involve in any way. – Marko Topolnik Feb 22 '13 at 14:29
  • I think this is barking up the right tree, but what is needed is `"LogParser"` instead of `"cmd /K start"`. Then the output stream can be used to communicate with the LogParser process. If the OP wants the LogParser to run in a separate cmd window, though, that ain't going to happen. – Theodore Norvell Feb 22 '13 at 14:56
0

As I said in my comment - 'They are executed as separate commands, they are not related just because you executed one before the other'

From the Runtime.exec( string ) javadoc -

Executes the specified command and arguments in a separate process.

You need to chain the commands together to get cmd to process your command, I believe you need to use the \k flag to specify what commands you need executed on the command line.

Runtime rt = Runtime.getRuntime();  
String start = "cmd /k ";
String cmd = "LogParser;\n" Select top 10 * into c:\temp\test9.csv from application";
rt.exec(start + cmd);

Not tested as I don't have windows, but it should be similar to this.

Tom
  • 15,798
  • 4
  • 37
  • 48
  • OP needs the `select` command passed as typed input to `LogParser`. – Marko Topolnik Feb 22 '13 at 14:20
  • Ok I tried this & compiles fine but nothing seems to happen Runtime rt = Runtime.getRuntime(); String start = "cmd /k "; String cmd = "LogParser"; String cmd2 = "Select top 10 * into c:\temp\test9.csv from application"; rt.exec(start + cmd + cmd2); – user2065481 Feb 22 '13 at 14:45
0

Why not use this:

String[] cmd = { "cmd /c", "LogParser",
        "Select top 10 * into c:\temp\test9.csv from application" };
rt.exec(cmd);

Find more info about the exec method here.

  • `select...` is a command accepted through the standard input by `LogParser`. – Marko Topolnik Feb 22 '13 at 14:24
  • String[] cmd = { "cmd /c", "LogParser", "Select top 10 * into c:\temp\test9.csv from application" };This returns a compile error on execution (cannot find the file specified) – user2065481 Feb 22 '13 at 14:56
0

You'll first need to start a process as you do in your first two lines of code, but don't use start because that spawns a separate process and returns immediately. Use just LogParser instead, or whatever will make your LogParser process start without involving cmd. After that you need to retrieve the OutputStream of the Process object created by exec and write your select command into it. You will also need to read from the Processs InputStream to see the response. None of this will be visible in a separate command-prompt window; you'll need to process everything through Java and it will involve some multithreading as well.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Thanks Marko. you seem to know the most about this issue. I would assume your solution works but that sound difficult to say the least notwithstanding the fact I have 3 months Java experince to my name – user2065481 Feb 22 '13 at 15:02
  • Try to see if you can make `LogParser` take your `select` as an argument. That would make things a lot simpler. Failing that, see if you can make it read its commands from a file. Maybe you could also try something like `echo "select ..." | LogParser`. – Marko Topolnik Feb 22 '13 at 15:25