How to invoke a shell script file in Java

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.