为 ProcessBuilder 设置工作目录不起作用

Setting working directory for ProcessBuilder is not working

我正在尝试使用 ProcessBuilder 运行 位于 C:/Software/ 的名为 test.pdf 的文件。以下是我的代码

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

我在 Whosebug 中检查了这个 How to set working directory with ProcessBuilder 线程。但是没有任何运气。任何人都可以帮忙吗?谢谢

使用以下代码:

        String fileToOpen = "test.pdf";
        List<String> command = new ArrayList<String>();
        command.add("rundll32.exe");
        command.add("url.dll,FileProtocolHandler");
        command.add(fileToOpen);

        ProcessBuilder builder = new ProcessBuilder();
        builder.directory(new File("C://Software//"));
        builder.command(command);

        builder.start();

它将打开您的 pdf。
如果要打开同一目录下的其他文件,只需更改文件名即可。