- 论坛徽章:
- 0
|
源代码在后面,控制台的输出是
0err,no comiled file foundF:\c\hello.exejava.lang.IllegalMonitorStateException
很奇怪,因为输出0,说明程序执行到了我的process函数的最后一个try()catch()块,但是给出一个异常却是在第三个try()catch块中出现的,
另外我对这个IllegalMonitorStateException异常也比较陌生,看doc文档的描述也很不是很懂.请问问题出在哪里,谢谢了.
package acmoj;
import java.io.*;
/**
*
* this class can run a test ,and get the result about the test.
* @version 2008/10/14
* @author lich
*
*/
public class test implements Runnable {
private String source;
private int type;
private int result;
/**
* this will new a porcsee and runing it will get the result
* @param s string means the place of the sourse file of test.
* @param t int.<br>
* 1 means using c and file I/O.<br>
* 2 means using c++ and file I/O.<br>
* 3 means using java and file I/O.<br>
* 4 means using c and console I/O.<br>
* 5 means using c++ and console I/O.<br>
* 6 means using java and console I/O<br>
*/
public test(String s,int t){
source=s;
type=t;
}
/**
* return the result about the test
*
* @return int<br>
* 0 accept <br>
* 1 wrong answer <br>
* 2 compile error <br>
* -1 err,file do not exist.<br>
*/
public int getresult(){
return result;
}
private int process(){
String cmd="";
//try to compile the input file
try{
if(type==2){
cmd="g++ -o "+source+" "+source+".cpp";
Process p=Runtime.getRuntime().exec(cmd);
p.waitFor();
p.destroy();
}
}catch(Exception e){
//this exception should never happen
System.out.print("err,no file found");
return -1;
}
//try to run the compiled programe
try{
if(type==2){
cmd=source+".exe";
Process p=Runtime.getRuntime().exec(cmd);
this.wait(1000);
p.destroy();
}
}catch(Exception e){
//if this exception happen means the comile has some errs;
System.out.print("err,no comiled file found"+cmd+e.toString());
return 2;
}
//try to compare the program output and the std output
try{
String stdout,out;
stdout=source+".sout";
out=source+".out";
FileInputStream stdStream = new FileInputStream(stdout);
FileInputStream file=new FileInputStream(out);
int tmp;
while((tmp=stdStream.read())!=-1){
if(tmp!=file.read()){
return 1;
}
}
stdStream.close();
file.close();
return 0;
}catch(Exception e){
return 1;
}
}
/**
* when it runs ,it will try to do things and you can use getresult() to get the result.
* @see #getresult()
*/
public void run() {
result=process();
}
/**
* this main function is used for test.
* @param args the test file names
*/
public static void main(String[] args) {
test t=new test("F:\\c\\hello",2);
new Thread(t).start();
System.out.print(t.getresult());
}
} |
|
|