0

Edited:

I am trying to open a file that is stored in a project file SRC. I am getting a "File not found" exception and I really don't understand why. The file is a docx and I have dropped it in eclipse into my project's src folder.

Here is the code where I try to open the file:

JButton documentationButton = new JButton("Helpfile");

        documentationButton.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent ae){

                Desktop desktop = null;
                if(Desktop.isDesktopSupported()){
                    desktop = Desktop.getDesktop();
                }

                try {
                    ClassLoader classLoader = getClass().getClassLoader();
                    File fi = new File(classLoader.getResource("ReturnBuilderDocumentation.docx").getFile());

                    String string = "file:/src/ReturnBuilderDocumentation.docx";

                    desktop.open(new File(URI.create(string)));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



            }

        });

Here is the exception generating:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file: \src\ReturnBuilderDocumentation.docx doesn't exist.
    at java.awt.Desktop.checkFileValidation(Unknown Source)
    at java.awt.Desktop.open(Unknown Source)
    at nachamultifive.GUIs.MainMenu$3.actionPerformed(MainMenu.java:172)
    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.plaf.basic.BasicButtonListener.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$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)

This file is clearly in my project src so I have no idea why the error is generating. Any help is greatly appreciated.

UPDATE:

I also tried with backslashes and another error was generated.

Code:

String string = "\\src\\ReturnBuilderDocumentation.docx";

Error:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Illegal character in opaque part at index 5: file:\src\ReturnBuilderDocumentation.docx
    at java.net.URI.create(Unknown Source)
    at nachamultifive.GUIs.MainMenu$3.actionPerformed(MainMenu.java:172)
    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.plaf.basic.BasicButtonListener.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$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)
Caused by: java.net.URISyntaxException: Illegal character in opaque part at index 5: file:\src\ReturnBuilderDocumentation.docx
    at java.net.URI$Parser.fail(Unknown Source)
    at java.net.URI$Parser.checkChars(Unknown Source)
    at java.net.URI$Parser.parse(Unknown Source)
    at java.net.URI.<init>(Unknown Source)
    ... 38 more
jesric1029
  • 698
  • 3
  • 10
  • 33
  • An absolute path contains the root directory, e.g. `C:\mypath\to\a\file.txt`. A relative path starts from some working directory, e.g. `mypath\to\a\file.txt`. Are you using a Windows operating system, by chance? Forward slashes ( / ) are for Unix systems. Backslashes ( \ ) are for Windows. – OneCricketeer Jan 14 '16 at 16:46
  • I was using "/". I thought that was okay since I was working directly out of my project folder. I just tried using backslashes with the escape sequence first like String string = "\\src\\ReturnBuilderDocumentation.docx"; and got unresolved compilation problem. I'm using Windows. – jesric1029 Jan 14 '16 at 16:49
  • Sure. The real problem, through, is that when you actually run the program, the Java files that are in `src` get compiled as class files and are placed in `bin` in Eclipse or I think `target` in IntelliJ. So, where you are trying to load the file from would like for the file in `bin/src`, for example. – OneCricketeer Jan 14 '16 at 16:57
  • 1
    I think what you currently have is okay, just do `desktop.open(fi)` instead of making a URI for that string – OneCricketeer Jan 14 '16 at 16:59
  • I tried the desktop.open(fi) and it works but when I compile my project and create a JAR I find that if I delete the docx from my PC it no longer opens using the button. Any reason for this? How can I make it read from the JAR and not from the computer. – jesric1029 Jan 14 '16 at 17:04
  • Are you sure the docx file is actually inside the JAR? (Open it with WinZip, or similar to verify) – OneCricketeer Jan 14 '16 at 17:07
  • Yes it is. Oddly enough if I have a copy of the docx on my desktop it will open but if not it will not open. The file is inside of the JAR though. – jesric1029 Jan 14 '16 at 17:12
  • Is there another way to open it that would open from JAR? – jesric1029 Jan 14 '16 at 17:16
  • Actually I found this quoted information in another question: "This is because regular Windows applications are only able to open files that lie directly in a filesystem or are reachable by a URL (http). Word does know nothing about JAR files, the best it could do would be to open the jar file and show that content. It knows nothing about the internal structure and that somewhere in that file a word file is hidden. If your problem is solved you can accept the answer and / or rate it." If you know anything different let me know please if not thanks for your help! – jesric1029 Jan 14 '16 at 17:17
  • Interesting.. and yeah, I misspoke after seeing this post http://stackoverflow.com/questions/550329/how-to-open-a-file-with-the-default-associated-program – OneCricketeer Jan 14 '16 at 17:19

2 Answers2

2

This code is the problem because it can't find that file at the given path

String string = "file:/src/ReturnBuilderDocumentation.docx";
desktop.open(new File(URI.create(string)));

Reason being is that the src folder doesn't really exist after the code is compiled.

To get the file resource post-compilation, you have to use

getClass().getClassLoader().getResource()

Which you have correctly done here

ClassLoader classLoader = getClass().getClassLoader();
File fi = new File(classLoader.getResource("ReturnBuilderDocumentation.docx").getFile());

So to open the file, you should simply have to do

desktop.open(fi);

Instead of

desktop.open(new File(URI.create(string)));
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

Check this answer. I don't think URIs can have a space, which is character 26 in your file name. File can accept a String, you could take out the URI and replace it with just the file name, or try to change the URI

Community
  • 1
  • 1
kneedhelp
  • 555
  • 4
  • 18
  • I tried removing the spaces and it did work but now I have an error that says the URL must be absolute. Any idea what this means? – jesric1029 Jan 14 '16 at 16:31