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 ());
}
}
}