我正在用 java 编写一个程序,它将执行 winrar 并将我的 jar 文件解压缩h:\myjar.jar
到文件夹h:\new
中。我的java代码是这样的
import java.io.File;
import java.io.IOException;
public class MainClass {
public static void main(String[] args) {
Runtime r = Runtime.getRuntime();
Process p = null;
try {
File dir = new File("C:/Program Files/WinRAR");
p = r.exec("winrar x h:\\myjar.jar *.* h:\\new", null, dir);
} catch (IOException e) {
e.printStackTrace();
}
}
}
当我执行此操作时,我收到此错误
java.io.IOException: Cannot run program "winrar" (in directory "C:\Program Files\WinRAR"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at MainClass.main(MainClass.java:16)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 4 more
谁能告诉我为什么会遇到这样的问题。我需要在代码中进行哪些更改才能使其正常工作?