当我用下面的语句执行unix指令删除文件的时候,因为Temp目录下面包含的文件太多(20000左右),所以实际执行时间很长,但是在程序里根本没执行完这条指令就跳过去了(执行该语句所用时间总是0)。所以在这里请教高手如何强制使该语句执行完毕下面程序再接着走?
...
long startTime = System.currentTimeMillis();
String command="rm -rf Temp/*.*";
Process proc = Runtime.getRuntime().exec(command);
long endTime = System.currentTimeMillis();
long totalTime = (endTime-startTime)/1000;
System.out.println("Take Time:"+totalTime);//总是返回0
if(proc.waitFor()!=0){
 System.out.println("Command Execution Failed!");}
...

解决方案 »

  1.   

    proc.waitFor()好像不管用,我试过了。请高手不吝赐教,谢谢!
      

  2.   

    Runtime.exec(“文本“)文本的内容必须在DOS或平台上可执行,如Runtime.exec(”move c:\\a.txt c:\\b\\”)就肯定执行不了,所以只能用拆衷的办法,把move c:\a.txt c:\b\写在一个批处理文件T内,然后执行:Runtime.exec(”t.bat:”)
      

  3.   

    楼主的实现方法正好犯了上面的错误,你可以把你的命令写一个批处理文件,然后再用exec方法执行这个文件
      

  4.   

    我的程序实际上是:
    long startTime = System.currentTimeMillis();
    String command="./delFile.bin  Temp/*.*";
    Process proc = Runtime.getRuntime().exec(command);
    long endTime = System.currentTimeMillis();
    long totalTime = (endTime-startTime)/1000;
    System.out.println("Take Time:"+totalTime);//总是返回0
    if(proc.waitFor()!=0){
    System.out.println("Command Execution Failed!");}
    delFile.bin 内容
    rm -f $1
    echo $1 deleted successfully!delFile.bin属性755
    可同样还是出现问题啊!
      

  5.   

    I finally got the solution at:
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.htmlThank you all for help!