我正在尝试使用ProcessBuilder运行一个名为test.pdf的文件,该文件位于C:/ Software /。以下是我的代码
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线程。但是没有运气。有人可以帮忙吗?谢谢
答案 0 :(得分:0)
使用以下代码:
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文件。
如果要在同一目录中打开其他文件,只需更改文件名。