Source:
Public StaticString CreateFile () {String cmd ="Echo\"Hhahah\"> F:\\AAA\\Azzz.txt "; File File =NewFile ("C:\\Windows\\System32 "); StringBuilder result =NewStringBuilder ();Try{//execute the specified string command in a separate process processes process = Runtime.getruntime (). exec (CMD);intExecresult = Process.waitfor (); System. out. println ("Execution results:"+execresult); BufferedReader input =NewBufferedReader (NewInputStreamReader (Process.getinputstream ())); String line =""; while(line = Input.readline ())!=NULL) {System. out. println (line);
Result.append (line);
} input.close (); }Catch(IOException e)
{E.printstacktrace (); }Catch(Interruptedexception e)
{E.printstacktrace (); } System. out. println ("The string result is:"+result); return""; }Public static voidMain (string[] args) {CreateFile ();}
The original program, such as on, run when the error message is as follows:
Java.io.IOException:Cannot Run Program "echo": CreateProcess error=2, the system could not find the specified file.
String result: at Java.lang.ProcessBuilder.start (processbuilder.java:1047) at Java.lang.Runtime.exec (runtime.java:617) At Java.lang.Runtime.exec (runtime.java:450) at Java.lang.Runtime.exec (runtime.java:347) at Cn.huimin100.pay.center.service.support.util.Test.createFile (test.java:22) at Cn.huimin100.pay.center.service.support.util.Test.main (test.java:42) at Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native method) at Sun.reflect.NativeMethodAccessorImpl.invoke ( nativemethodaccessorimpl.java:57) at Sun.reflect.DelegatingMethodAccessorImpl.invoke ( delegatingmethodaccessorimpl.java:43) at Java.lang.reflect.Method.invoke (method.java:606) at Com.intellij.rt.execution.application.AppMain.main (appmain.java:147) caused by:java.io.IOException:CreateProcess
error=2, the system could not find the specified file. At Java.lang.ProcessImpl.create (Native method) at java.lang.processimpl.<init> (processimpl.java:385) at Java.lang.ProcesSimpl.start (processimpl.java:136) at Java.lang.ProcessBuilder.start (processbuilder.java:1028) ...
Ten more
Description, when invoking the cmd command in Java, you need to specify
That is, the command that is invoked needs to indicate whether CMD is running the command
Modify:
\" Hhahah \" > F: \\ AAA \\ azzz.txt ";
Indicate:
The Runtime.exec method creates a native process and returns an instance of the process subclass that can be used to control the process and obtain relevant information