java--invoke Linux and Windows command-line execution __web

Source: Internet
Author: User
Tags readline stringbuffer

1. Project Requirements

Need to invoke another program on the back end and get the return result


2. First version of the environment: Windows

CMDSTR is the command parameter public boolean excutecmd (String cmdstr) {string[] cmd = new string[]{"cmd.exe", "
        /C ", cmdstr};
        BufferedReader br = null;

        String line = null;
        Runtime Runtime = Runtime.getruntime ();
            try {Process pro = runtime.exec (cmd);
            StringBuffer sbout = new StringBuffer (1000);

            br = new BufferedReader (New InputStreamReader (Pro.getinputstream ()));
                if (line = Br.readline ())!= null) {string[] Sourcestrarray=line.split (":");
                System.out.println (sourcestrarray[0]+ "=" +sourcestrarray[1]);
                if (Sourcestrarray[0].equals ("Status") &&sourcestrarray[1].equals ("SUCCESS")) {return true;
                }else{return false;
        return false;
            catch (Exception e) {System.out.println ("Excute Error:" + e);
        return false;
  }  } 




3. The second version of the environment: Linux
Reference Blog: http://www.linuxidc.com/Linux/2012-04/58416.htm
public class Linuxstart {
static String jarfile= "Java-jar/home/ictuser/test_linux/helloworld.jar";
Static string[] cmd1 = {"sh", "-C", jarfile};
String[] cmd2 = {"sh", "-C", "Lsmod |grep Linuxvmux"};

public static void Main (string[] args) {
//TODO auto-generated method Stub
doCmd ();
}

public static void DoCmd () {
StringBuffer buf = new StringBuffer (1000);
    String rt= "-1";
 try {
  Process pos = Runtime.getruntime (). exec (CMD1);
  
  InputStreamReader ir = new InputStreamReader (Pos.getinputstream ());
   LineNumberReader input = new LineNumberReader (IR);
   String ln= "";
   while (ln =input.readline ())!= null) {
       buf.append (ln+ "<br>");
       SYSTEM.OUT.PRINTLN (LN);
   }
   RT = Buf.tostring ();
   Input.close ();
   Ir.close ();
   
 } catch (Java.io.IOException e) {
  rt=e.tostring ();
 } catch (Exception e) {
 System.out.println (e.tostring ());
 }
}
}


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.