//试试这个import java.io.*; public class Pnotepad {
public static void main(String args[]) {
String s;
try {
Process myProcess = Runtime.getRuntime().exec ("D:\\jdk1.3\\bin\\javac d:\\abc.java");
BufferedReader in = new BufferedReader(new InputStreamReader(myProcess.getInputStream()));
while ((s = in.readLine()) != null) {
System.out.println(s);
}
System.exit(0);
}
catch (IOException e) {
System.out.println("Exception: "+ e);
System.exit(-1);
}
}
}
public static void main(String args[]) {
String s;
try {
Process myProcess = Runtime.getRuntime().exec ("D:\\jdk1.3\\bin\\javac d:\\abc.java");
BufferedReader in = new BufferedReader(new InputStreamReader(myProcess.getInputStream()));
while ((s = in.readLine()) != null) {
System.out.println(s);
}
System.exit(0);
}
catch (IOException e) {
System.out.println("Exception: "+ e);
System.exit(-1);
}
}
}
等会吧!
c:\>D:\\jdk1.3\\bin\\javac d:\\abc.java能行吗?
要用classpath参数。
Process myProcess = Runtime.getRuntime().exec ("D:\\jdk1.3\\bin\\javac d:\\abc.java");
这段程序如果产生异常,是由主线程捕获到呢?还是由新建的进程myProcess捕获呢?
我认为因该由myProcess来捕获,但是它始终不活不到,现在我总于相通了,那是因为你的那个进程根本就没生成!所以他自然就捕获不到异常啦!
Runtime.getRuntime().exec (参数1,参数2);也就是说只有这个进程建立,他才能捕获
自己的异常,那他什么时候才建立呢。我试了一下,自有当参数一,也就是你要运行的命令
能正常的运行,这样你的进程就建立起来了。如果参数二出错,也就是参数一运行的参数出错
那么,这个进程就可以捕获到异常了!
你可以试试下面的程序看看!import java.io.*;
public class Pnotepad
{
public static void main(String args[])
{
try
{
String s;
Process myProcess = null;
myProcess = Runtime.getRuntime().exec ("javac wxyxl.java"); //wxyxl.java这个文件名可以随便取,但是必须的是实际不存在的 ,因为你要捕获异常!
BufferedReader in = new BufferedReader(new
InputStreamReader(myProcess.getErrorStream()));
while ((s = in.readLine()) != null)
{
System.out.println("这是进程myProcess捕获到的异常:"+s);
}
}catch(Exception e){System.out.println("这是主线程捕获到的异常");}
}
} 你试试看吧!我已经成功了!
如不行,我们再讨论!