【问题标题】:Opening PDF only works in netbeans打开 PDF 仅适用于 netbeans
【发布时间】:2014-03-08 01:11:15
【问题描述】:

我编写了一个创建 PDF 文档的程序。创建后,PDF 必须打开才能打印。

我有以下代码,但它仅在我从 netbeans 启动时才有效。谁能给我一些指点?

 public void openPDF()
    {
        try {
            System.out.println("Opening PDF");
            File file = new File(pdfPath+pdfName);
            String absolutePDFpath = file.getAbsolutePath().replace(""+(char)92,""+(char)92+(char)9);
            System.out.println("Path = "+absolutePDFpath);
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + absolutePDFpath);
        } 
        catch (IOException ex) 
        {
            JOptionPane.showMessageDialog(null, "Er is een fout opgetreden tijdens het openen van het PDF"
                    + " document\nFoutcode: 0xFF05");
            Logger.getLogger(PrintJob.class.getName()).log(Level.SEVERE, null, ex);
        }

【问题讨论】:

  • 你用什么其他方式来启动程序?在这种情况下你会遇到什么错误?
  • 请提供有关从外部 netbeans 运行时看到的异常的详细信息。

标签: java pdf file-io runtime exec


【解决方案1】:

请参阅Desktop.open(File),了解跨不同平台打开 PDF 的方法。像这样的..

File file = new File(pdfPath+pdfName);
Desktop.getDesktop().open(file);

如果应用程序。需要支持 Java 1.5 或更早版本(Desktop 可用之前),坚持使用exec(),但实现When Runtime.exec() won't 的所有建议。

为了可靠运行,使用Process 需要程序员做很多事情。该代码没有个。

【讨论】:

    【解决方案2】:

    您的代码可能不起作用,因为您没有指定环境变量。应该是这样的:

    Runtime.getRuntime().exec("command to execute", env[]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-17
      • 2020-05-21
      • 2023-01-30
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多