[code]
public class Test {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
try {
Process proc = rt.exec("C:\\Program Files\\Internet Explorer\\iexplore.exe");
int exitVal = proc.exitValue();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
但是这样允许会出现一个异常:
Exception in thread "Main Thread" java.lang.IllegalThreadStateException: process has not exited
问题原因:
因为程序还没有退出,所以proc.exitValue();程序退出的值当然没有啦。。
怎么改呢。。。
public class Test {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
try {
Process proc = rt.exec("C:\\Program Files\\Internet Explorer\\iexplore.exe");
int exitVal = proc.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这里主要是要学习用运行时对象来执行外面的程序。。。
下面换成javac试试
public class Test {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
int exitVal = 0;
try {
Process proc = rt.exec("javac");
exitVal = proc.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(exitVal);
}
}
允许后发现javac死在这里了,因为proc.waitFor一直在等待关闭,而现在javac这样调用的语法有问题,所以这个程序会先输出错误信息再输出退出的val,但是这时候它想输出错误信息都输不出来,所以程序一直挂在这里。。。改的方法就是让错误信息先输出来呀~~~~~~~
public class Test {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
int exitVal = 0;
try {
Process proc = rt.exec("javac");
exitVal = proc.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(exitVal);
}
}
//我改
public class Test {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
int exitVal = 0;
try {
Process proc = rt.exec("javac");
InputStream es = proc.getErrorStream();
InputStreamReader reader = new InputStreamReader(es);
BufferedReader br = new BufferedReader(reader);
StringBuffer sb = new StringBuffer();
String l = "";
while((l = br.readLine()) != null){
sb.append(l+"\n");
}
System.out.println(sb.toString());
exitVal = proc.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(exitVal);
}
}
* 永远要在调用waitFor()方法之前读取数据流
* 永远要先从标准错误流中读取,然后再读取标准输出流
于是将waitFor()方法放在读取数据流后调用,目前没有发现什么问题。
正好解决了我心中的疑问,非常感谢!
我们的程序一开始就是exec完了接着waitFor(),但bat文件执行不完整:
Process proc = Runtime.getRuntime().exec(cmd);
proc.waitFor();
后面的build中在waitFor()之前读取了数据流,bat文件就可以完整执行了:
Process proc = Runtime.getRuntime().exec(cmd);
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "Error");
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "Output");
errorGobbler.start();
outputGobbler.start();
proc.waitFor();
class StreamGobbler extends Thread {
InputStream is;
String type;
StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
if (type.equals("Error"))
LogManager.logError(line);
else
LogManager.logDebug(line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
[/code]
public class Test {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
try {
Process proc = rt.exec("C:\\Program Files\\Internet Explorer\\iexplore.exe");
int exitVal = proc.exitValue();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
但是这样允许会出现一个异常:
Exception in thread "Main Thread" java.lang.IllegalThreadStateException: process has not exited
问题原因:
因为程序还没有退出,所以proc.exitValue();程序退出的值当然没有啦。。
怎么改呢。。。
public class Test {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
try {
Process proc = rt.exec("C:\\Program Files\\Internet Explorer\\iexplore.exe");
int exitVal = proc.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这里主要是要学习用运行时对象来执行外面的程序。。。
下面换成javac试试
public class Test {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
int exitVal = 0;
try {
Process proc = rt.exec("javac");
exitVal = proc.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(exitVal);
}
}
允许后发现javac死在这里了,因为proc.waitFor一直在等待关闭,而现在javac这样调用的语法有问题,所以这个程序会先输出错误信息再输出退出的val,但是这时候它想输出错误信息都输不出来,所以程序一直挂在这里。。。改的方法就是让错误信息先输出来呀~~~~~~~
public class Test {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
int exitVal = 0;
try {
Process proc = rt.exec("javac");
exitVal = proc.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(exitVal);
}
}
//我改
public class Test {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
int exitVal = 0;
try {
Process proc = rt.exec("javac");
InputStream es = proc.getErrorStream();
InputStreamReader reader = new InputStreamReader(es);
BufferedReader br = new BufferedReader(reader);
StringBuffer sb = new StringBuffer();
String l = "";
while((l = br.readLine()) != null){
sb.append(l+"\n");
}
System.out.println(sb.toString());
exitVal = proc.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(exitVal);
}
}
* 永远要在调用waitFor()方法之前读取数据流
* 永远要先从标准错误流中读取,然后再读取标准输出流
于是将waitFor()方法放在读取数据流后调用,目前没有发现什么问题。
正好解决了我心中的疑问,非常感谢!
我们的程序一开始就是exec完了接着waitFor(),但bat文件执行不完整:
Process proc = Runtime.getRuntime().exec(cmd);
proc.waitFor();
后面的build中在waitFor()之前读取了数据流,bat文件就可以完整执行了:
Process proc = Runtime.getRuntime().exec(cmd);
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "Error");
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "Output");
errorGobbler.start();
outputGobbler.start();
proc.waitFor();
class StreamGobbler extends Thread {
InputStream is;
String type;
StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
if (type.equals("Error"))
LogManager.logError(line);
else
LogManager.logDebug(line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
[/code]