Passing a string to the Windows command line

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 a command window, but lines are not transmitted after opening. Can someone tell me why this code will not put lines in the command window?

+5
source share
5 answers

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

Thus, the other command is treated as split.

+2
source

Use OutputStreamWriterand write to the input stream of the created process.

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

In addition, the reason for using / K:

/ K Run the command, and then return to the CMD prompt.

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

+1
source

: " , , "

Runtime.exec( string ) javadoc -

.

, cmd , , \k, , .

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);

, , .

0

:

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

exec .

0

, , start, . LogParser -, LogParser cmd. OutputStream Process, exec, select. Process InputStream, . ; Java, .

0

All Articles