|
常用链接
留言簿(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中,远程执行命令。 有对上面的问题了解的看客请解我的疑惑。
|
|