Unable to open PDF file using java command line

2020-04-22 01:28发布

I am trying to open a PDF file from java using the command line argument as

String command = "cmd /c start AcroRd32.exe \"" + dir + "\"";

But i am getting an error when the file will be automatically opened as

"Windows cannot find 'acroRd32.exe'. Please make sure you typed the correct name."

However i am able to open it manually without using the command line/java.

Please help me out.

4条回答
贼婆χ
2楼-- · 2020-04-22 01:59

I am not sure that this is really useful but it works to open pdf file.

String FileName="C:/name.pdf";//Write your complete path here
try {
       Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + FileName);
    } catch (IOException ex) {
             Logger.getLogger(ClassName.class.getName()).log(Level.SEVERE, null, ex);
    }
查看更多
▲ chillily
3楼-- · 2020-04-22 02:02

I strongly recommend that you do it this way instead.

java.io.File file = new java.io.File("c:/some/file.pdf");
java.awt.Desktop.open(file);

If you still want to run "AcroRd32.exe" (or some other "command") use a ProcessBuilder, with something like this -

ProcessBuilder pb = new ProcessBuilder("AcroRd32.exe", dir);
Process p = pb.start();
查看更多
\"骚年 ilove
4楼-- · 2020-04-22 02:04
String cmds[] = new String[] {"cmd", "/c",  "C:\\test.pdf"};  
        Runtime.getRuntime().exec(cmds);  
查看更多
太酷不给撩
5楼-- · 2020-04-22 02:10
START C:\temp\My_PDF_File.pdf use this command for opening from command line

other wise use class desktop

查看更多
登录 后发表回答