Java Execute shell/cmd command

Source: Internet
Author: User

Try{Process P=runtime.getruntime (). exec ("chmod 777/home/bomb/movetoy/webroot/a.sh" );                        P.waitfor (); Process Pro= Runtime.getruntime (). exec ("/home/bomb/movetoy/webroot/./a.sh" +num);            Pro.waitfor (); System.out.println ("run"); Java.io.InputStream out=Pro.getinputstream (); inti; String a= ""; String b= ""; intN=0; ArrayList<String> tmp =NewArraylist<string>() ;  while((i = Out.read ())! = 1) {                CharC= (Char) i; if(i==10)                {                    //System.out.println (a);Tmp.add (a); A=""; Continue; }                if(i!=13&&i!=10)                { //System.out.println (n++ + ":" +c+ "" +i ");A + =c; }            }                        intlen=tmp.size (); //Extracting dissipation valuesString [] tmphaosan=NewString [len];  for(i=0;i<len;i++) {tmphaosan[i]=tmp.get (i). Split ("") [1]; //System.out.println (tmp.get (i). split ("") [1].split ("\\n") [0]);} String ans=""; String Anss="";  for(i=1;i<len;i++) {ans=""; String T1=tmp.get (i-1). Split ("") [0]; String T2=tmp.get (i). Split ("") [0];  for(intJ=0;j<t1.length (); J + +)                {                    //Make sure the space is behind                    if(t2.charat (j) = = ' E ')                    {                        //System.out.println (j+1+ "" +t1.charat (j) + "" +t2.charat (j));ans= (j+1) + "," +ans; }                    if(t1.charat (j) = = ' E ') {ans+ = (j+1); Ans+=","; }} ans+=tmphaosan[i]; Ans+ = '; '; Anss+=ans; }            //System.out.println (anss);PrintWriter PW =Response.getwriter ();        Pw.write (anss); } Catch(Exception E) {System.out.println (num);        E.printstacktrace (); }

A child process is generated from the runtime class, waiting for the child thread to complete Execution. And the output of the external program is read by intercepting the standard output Stream.

Java Execute shell/cmd command

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.