|
常用鏈接
留言簿(3)
隨筆分類(53)
文章分類(6)
收藏夾(11)
消遣站點
搜索
最新評論

Powered by: 博客園
模板提供:滬江博客
|
|
|
|
|
發新文章 |
|
|
- 用startProcessByReader()執行javac命令沒有問題,而執行java命令就會阻塞,不知道怎么回事。startProcess()函數就不會產生這樣的問題。難道本地命令執行時返回的不能用Reader?
- 需研究jdk的源碼。不知道這里的Runtime.getRuntime().exec()函數是怎么實現的,看看。
- InputStream出來的是byte,在進行char轉型后得到的字符是ASCII編碼,須用GBK再轉換一次
import java.io.*;
import java.util.*;

 public class ExecNatComm {

 /** *//**
* Starts a native process
*
* @param command
* the command to start the process
* @param dir
* the dir in which the process starts
*/
 public String startProcess(String command, String dir) throws IOException {
StringBuffer ret = new StringBuffer();
String[] comm = new String[3];
comm[0] = "cmd";
comm[1] = "/C";
comm[2] = command;
long start = System.currentTimeMillis();
 try {
Process ls_proc = Runtime.getRuntime().exec(comm, null, new File(dir));
// Get input and error streams
BufferedInputStream ls_in = new BufferedInputStream(ls_proc.getInputStream());
BufferedInputStream ls_err = new BufferedInputStream(ls_proc.getErrorStream());
boolean end = false;
 while (!end) {
int c = 0;
 while ((ls_err.available() > 0) && (++c <= 1000)) {
ret.append((char)(ls_err.read()));
}
c = 0;
 while ((ls_in.available() > 0) && (++c <= 1000)) {
ret.append((char)(ls_in.read()));
}
 try {
ls_proc.exitValue();
while (ls_err.available() > 0)
ret.append((char)(ls_err.read()));
while (ls_in.available() > 0)
ret.append((char)(ls_in.read()));
end = true;
 } catch (IllegalThreadStateException ex) {
// Process is running
}
// The process is not allowed to run longer than given time.
 if (System.currentTimeMillis() - start > 20000) {
ls_proc.destroy();
end = true;
ret.append("!!!! Process has timed out, destroyed !!!!!");
}
 try {
Thread.sleep(50);
 } catch (InterruptedException ie) {

}
}
 } catch (IOException e) {
ret.append("Error: " + e);
}
String gbkRet =ret.toString();
gbkRet= new String(gbkRet.getBytes("ISO8859-1"), "gbk");
return gbkRet;
}

 public String startProcessByReader(String command, String dir) throws IOException {
StringBuffer ret = new StringBuffer();
String[] comm = new String[3];
comm[0] = "cmd";
comm[1] = "/C";
comm[2] = command;
long start = System.currentTimeMillis();
 try {
Process ls_proc = Runtime.getRuntime().exec(comm, null, new File(dir));
// Get input and error streams
InputStreamReader ls_in_reader = new InputStreamReader(ls_proc.getInputStream());
InputStreamReader ls_err_reader = new InputStreamReader(ls_proc.getErrorStream());
BufferedReader in_br = new BufferedReader(ls_in_reader);
BufferedReader err_br = new BufferedReader(ls_err_reader);

boolean end = false;
String line = null;
 while (!end) {
// Process is running
while((line=err_br.readLine())!=null) ret.append(line).append("\r\n");
while((line=in_br.readLine())!=null) ret.append(line).append("\r\n");
 try {
ls_proc.exitValue();
while((line=err_br.readLine())!=null) ret.append(line).append("\r\n");
while((line=in_br.readLine())!=null) ret.append(line).append("\r\n");
end = true;
 } catch (IllegalThreadStateException ex) {
// Process is running
}
// The process is not allowed to run longer than given time.
 if (System.currentTimeMillis() - start > 20000) {
ls_proc.destroy();
end = true;
ret.append("!!!! Process has timed out, destroyed !!!!!");
}
 try {
Thread.sleep(50);
 } catch (InterruptedException ie) {
}
}
 } catch (IOException e) {
ret.append("Error: " + e);
}
return (ret.toString());
}

 /** *//**
* @param args
* @throws IOException
*/
 public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
ExecNatComm exe = new ExecNatComm();
String ret = exe.startProcess("javadd", "d:");
System.out.println(ret);
ret = exe.startProcessByReader("javac","d:");
System.out.println(ret);
}
}

呵呵,這個可以用在jsp中,遠程執行命令。 有對上面的問題了解的看客請解我的疑惑。
|
|