我写了个程序,想去测试多个ip是否ping的通,老出问题,代码如下:public static void main(String[] args) throws IOException, InterruptedException {
File iplist= new File("F:\\IPList.txt");
FileReader reader = new FileReader(iplist);
BufferedReader br = new BufferedReader(reader);
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");
String filename = df.format(new Date(System.currentTimeMillis()));
File r= new File("F:"+File.separator+"ping"+File.separator+"pinging"+filename+"by3.txt");
System.out.println(r.getAbsolutePath());
String tempStr=null;
ArrayList<String[]> list=new ArrayList<String[]>();
while ((tempStr = (br.readLine()))!= null)
{
tempStr=tempStr.trim();
if(tempStr!=null){
String[] s = tempStr.split(",");
list.add(new String[]{s[0],s[1],""});
}
}
for(int i=0;i<list.size();i++){
String[] t=list.get(i);
Process p=Runtime.getRuntime().exec("ping "+t[1]+" -n 3 -w 1000");
Thread.currentThread().sleep(1500);
System.out.print(t[1]+" ");
if(p.exitValue()==0){ //这一行报错
System.out.println("pass");
}
System.out.println("error");
p.destroy();
}
}
异常如下F:\ping\pinging20091112_094013by3.txt
200.1.1.3 Exception in thread "main" java.lang.IllegalThreadStateException: process has not exited
at java.lang.ProcessImpl.exitValue(Native Method)
at test.TestIp.main(TestIp.java:42)
File iplist= new File("F:\\IPList.txt");
FileReader reader = new FileReader(iplist);
BufferedReader br = new BufferedReader(reader);
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");
String filename = df.format(new Date(System.currentTimeMillis()));
File r= new File("F:"+File.separator+"ping"+File.separator+"pinging"+filename+"by3.txt");
System.out.println(r.getAbsolutePath());
String tempStr=null;
ArrayList<String[]> list=new ArrayList<String[]>();
while ((tempStr = (br.readLine()))!= null)
{
tempStr=tempStr.trim();
if(tempStr!=null){
String[] s = tempStr.split(",");
list.add(new String[]{s[0],s[1],""});
}
}
for(int i=0;i<list.size();i++){
String[] t=list.get(i);
Process p=Runtime.getRuntime().exec("ping "+t[1]+" -n 3 -w 1000");
Thread.currentThread().sleep(1500);
System.out.print(t[1]+" ");
if(p.exitValue()==0){ //这一行报错
System.out.println("pass");
}
System.out.println("error");
p.destroy();
}
}
异常如下F:\ping\pinging20091112_094013by3.txt
200.1.1.3 Exception in thread "main" java.lang.IllegalThreadStateException: process has not exited
at java.lang.ProcessImpl.exitValue(Native Method)
at test.TestIp.main(TestIp.java:42)
System.out.println("pass");
}