private static String rarCmd = "C:\\Program Files\\WinRAR\\Rar.exe a ";   
    private static String unrarCmd = "C:\\Program Files\\WinRAR\\UnRar x ";  
    private String rarFileName="d:/rar/rar.rar";
    private String destDir="c:/rartemp/";
unrarCmd += rarFileName + " " + destDir;  
Runtime rt = Runtime.getRuntime();  
rt.exec(unrarCmd); 
File file = new File("c:/rartemp");
File [] f = file.listFiles();
为什么每次第一运行,f.length总是为1。而第二次运行的时候就正常了?

解决方案 »

  1.   

    使用jdk5.0测试正常,rar中文件大小为0时不计入f.length
      

  2.   

    rt.exec(unrarCmd);   
    你是不是认为,他运行完了才执行下面的语句啊?
    其实,这个命令发出去之后,程序马上执行后面的代码了,你那个unrarCmd 可能还根本没有来得及执行完毕呢!
      

  3.   

    Process p = rt.exec(unrarCmd); 
    p.waitFor();
      

  4.   

    谢谢了,但用p.waitFor();这个方法,压缩包里面的东西不能超过5个文件了,多了就不行了。
    我用 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
                   if( bufferedReader.readLine()!= null){
                   p.waitFor();
                   }
    判断它也不行,不知道为什么还是不行~
      

  5.   

    我只能提供一个思路,具体的编码你自己完成
    1 启动你的exec
    2 判断进程里面是否有winrar (tasklist)
    3 如果没有,等待1秒后再判断终止条件
    1 等待超过10秒,仍没有发现
    2 发现了,但在下次判断时有没有了!
      

  6.   

    我以前给人说过好多次了,p.waitFor(); 会造成死锁
    参看javadoc
    The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock. 
    最后一句话
    所以自己要清空输入输出流缓存
    即要自己调用p.getInputStream().read()等等
    具体不再多说了,LZ自己搜索以前的帖子看看吧