Java加载PDF文件文件不存在

时间:2015-08-20 16:38:30

标签: java file pdf not-exists

我试图在用户单击按钮时让我的Java应用程序打开PDF文件。但是,我得到下面的堆栈跟踪,说明该文件不存在。基本上我希望能够在用户进行选择时加载此文件。

下面我将有堆栈跟踪代码和路径的屏幕截图。

堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file: \RFBase-TD_Communications\src\pdf\RFTDAnalyzerHelpFile.pdf doesn't exist.
    at java.awt.Desktop.checkFileValidation(Unknown Source)
    at java.awt.Desktop.open(Unknown Source)
    at GUI.rfbgui.openPDF(rfbgui.java:787)
    at GUI.rfbgui.access$7(rfbgui.java:773)
    at GUI.rfbgui$6.actionPerformed(rfbgui.java:921)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

代码:

private static void openPDF()
{
    File pdfHelpFile = new File("/RFBase-TD_Communications/src/pdf/RFTDAnalyzerHelpFile.pdf");
    try
    {
        Desktop.getDesktop().open(pdfHelpFile);

    }catch(IOException ex)
    {
        ex.printStackTrace();
    }
}

enter image description here

2 个答案:

答案 0 :(得分:0)

File myFile = new File(getClass().getResource("/files/test.pdf").toURI());

if (Desktop.isDesktopSupported()) {
try {
    File myFile = new File("/path/to/file.pdf");
    Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
    // no application registered for PDFs
}

答案 1 :(得分:0)

我对如何处理这些情况有一些一般性的建议。文件是我开始学习编程时非常沮丧的事情之一。

  1. 使用 System.getProperty(" user.dir"); 这可能非常有用,尤其是当您不知道程序将从何处运行时,或者你有一个特定的文件结构。

  2. 在Java中,我通常建议使用" \"而不是" /"。

  3. 对您尝试加载的文件运行完整性检查。具体检查它是否为null,。isFile()等。你永远不知道你可能会得到什么,所以在意外崩溃你的程序之前采取一个高峰是好的。

  4. 以下是一些可能对您有帮助的类似问题的链接;

    How should I load files into my Java application?

    Getting the Current Working Directory in Java

    Getting the inputstream from a classpath resource (XML file)