CreateProcess error=2, The system cannot find the file specified

前端 未结 5 943
既然无缘
既然无缘 2020-11-29 04:58

I am writing a program in java which would execute winrar and unzip a jar file for me placed in h:\\myjar.jar into the folder h:\\new. My java code

5条回答
  •  旧巷少年郎
    2020-11-29 05:03

    I used ProcessBuilder but had the same issue. The issue was with using command as one String line (like I would type it in cmd) instead of String array. In example from above. If I ran

    ProcessBuilder pb = new ProcessBuilder("C:/Program Files/WinRAR/winrar x myjar.jar *.* new");
    pb.directory(new File("H:/"));
    pb. redirectErrorStream(true);
    
    Process p = pb.start();
    

    I got an error. But if I ran

    ProcessBuilder pb = new ProcessBuilder("C:/Program Files/WinRAR/winrar", "x", "myjar.jar", "*.*", "new");
    pb.directory(new File("H:/"));
    pb. redirectErrorStream(true);
    
    Process p = pb.start();
    

    everything was OK.

提交回复
热议问题