ProcessBuilder的java设置工作目录不工作
我正在尝试运行一个名为test的文件。pdf,位于C:/Software/使用ProcessBuilder。下面是我的代码
public static void main(String[] args) throws IOException {
ProcessBuilder pb = new ProcessBuilder("test.pdf");
pb.directory(new File("C:/Software/"));
pb.start();
}
我得到了以下例外
Exception in thread "main" java.io.IOException: Cannot run program "test.pdf" (in directory "C:\Software"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at com.test.Test.main(Test.java:12)
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)
... 2 more
我在stackoverflow中检查了这个How to set working directory with ProcessBuilder线程。但是没有任何运气。有人能帮忙吗?谢谢
# 1 楼答案
使用以下代码:
它将打开您的pdf
如果要打开同一目录中的其他文件,只需更改文件名即可