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?
The / C parameter means:Carries out the command specified by the string and then terminates.
Carries out the command specified by the string and then terminates.
Thus, the other command is treated as split.
Use OutputStreamWriterand write to the input stream of the created process.
OutputStreamWriter
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
: " , , "
Runtime.exec( string ) javadoc -
Runtime.exec( string )
.
, cmd , , \k, , .
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);
, , .
:
String[] cmd = { "cmd /c", "LogParser", "Select top 10 * into c:\temp\test9.csv from application" }; rt.exec(cmd);
exec .
exec
, , start, . LogParser -, LogParser cmd. OutputStream Process, exec, select. Process InputStream, . ; Java, .
start
LogParser
OutputStream
Process
select
InputStream