0

I am attempting to have my Java Application open a PDF file when the user clicks button. However, I get the stack trace below stating that the file doesn't exist. Bascially I would like to be able to load this file when the user makes the selection.

Below I will have the stack trace then the code and a screenshot of the path.

StackTrace:

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)

Code:

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

vector
  • 199
  • 1
  • 4
  • 17
  • I have also tried the path pdf/RFTDAnalyzerHelpFile.pdf – vector Aug 20 '15 at 16:39
  • possible duplicate of [Getting the inputstream from a classpath resource (XML file)](http://stackoverflow.com/questions/793213/getting-the-inputstream-from-a-classpath-resource-xml-file) – home Aug 20 '15 at 16:45
  • In case your resource (file) is in the classpath (e.g. src folder). you should use another method. See my comment above... – home Aug 20 '15 at 16:45

2 Answers2

0

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

or

if (Desktop.isDesktopSupported()) {
try {
    File myFile = new File("/path/to/file.pdf");
    Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
    // no application registered for PDFs
}
Anand Dwivedi
  • 1,452
  • 13
  • 23
0

I have some general advice for how to handle these situations. Files were one of the things I got very frustrated with when starting to learn to program.

  1. Use System.getProperty("user.dir"); This can be very helpful especially when you do not know where the program is going to be run from, or you have a specific file structure.

  2. In Java, I generally recommend using "\" instead of "/".

  3. Run a sanity check on the file you are attempting to load. Specifically check if it is null, .isFile(), etc. You never know what you might get back, so its good to take a peak before accidently crashing your program.

Here is some links for similar questions that might help you out;

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)

Community
  • 1
  • 1
Sh4d0wsPlyr
  • 948
  • 12
  • 28
  • I ran the .isFile() and I was returned false. Why am I having that returned? Am I not pointing to the file correctly? – vector Aug 20 '15 at 17:03
  • If it returns false, it means whatever it is, it is not a file. That could be because it was not found, or maybe its a folder, etc. This check is only there to make sure that before you run your code (and get a null pointer exception) that it verifies it can actually run the code. To answer your question - that is my guess. You likely don't have the correct path. – Sh4d0wsPlyr Aug 20 '15 at 17:27
  • @Sh4d0wsPlyr- Thanks for the response, I have my path listed above, can you help me determine the correct path, I feel like I am linking to the file correctly. – vector Aug 20 '15 at 17:30
  • I did this.java.net.URL link2=rfbgui.class.getResource("/pdf/RFTDAnalyzerHelpFile.pdf"); – vector Aug 20 '15 at 17:36