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?

5
  • They are executed as separate commands, they are not related just because you executed one before the other. Commented Feb 22, 2013 at 14:09
  • 1
    You are nowhere near a solution. I think your best bet is to create a text file with those commands and make LogParser accept commands from it. Commented Feb 22, 2013 at 14:12
  • Ok - but if I put them all into one string like Commented Feb 22, 2013 at 14:14
  • String[] cmd = {"cmd /c start", "LogParser", "Select top 10 * into c:\temp\test9.csv from application" }; the compiler doesn't like it Commented Feb 22, 2013 at 14:20
  • By the way, be sure to escape the backslashes: "Select top 10 * into c:\\temp\\test9.csv from application". Commented Feb 22, 2013 at 14:59

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

...and this will fail because cmd completes immediately after spawning a separate process.
What will happen with your proposal is that w will append to cmds standard input, instead of LogParsers -- which you don`t even involve in any way.
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.
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.

2 Comments

select... is a command accepted through the standard input by LogParser.
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)
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.

2 Comments

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
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.
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.

2 Comments

OP needs the select command passed as typed input to LogParser.
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.