0

I've browsed several questions on this site related to this issue and most of them say to use the getClass().getResource() method. However, when I do this, it causes an IllegalArgumentException in Eclipse. After I export my project as a runnable JAR, double-clicking doesn't open it and running it in Command Prompt gives me this:

Microsoft Windows [Version 10.0.19043.1645]
(c) Microsoft Corporation. All rights reserved.

C:\Users\benaa>cd C:\Users\benaa\Documents\School\2420\RouteFinder

C:\Users\benaa\Documents\School\2420\RouteFinder>java -jar RouteFinder.jar
javax.imageio.IIOException: Can't read input file!
        at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1308)
        at routeFinder.RouteApp.<init>(RouteApp.java:134)
        at routeFinder.RouteApp$1.run(RouteApp.java:55)
        at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
        at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
        at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
        at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
java.lang.NullPointerException
        at routeFinder.RouteApp.<init>(RouteApp.java:142)
        at routeFinder.RouteApp$1.run(RouteApp.java:55)
        at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
        at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
        at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
        at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

C:\Users\benaa\Documents\School\2420\RouteFinder>

Here is the statement in question:

BufferedImage img = null;
        try
        {
            img = ImageIO.read(new File("images/New Trax Map Edited.png"));
        } catch (IOException e)
        {
            e.printStackTrace();
        }

Again, I've tried the getClass.getResource(); method but it keeps throwing an IllegalArgumentException in Eclipse:

java.lang.IllegalArgumentException: input == null!

I've also tried reformatting so the image input uses a throws declaration at the beginning of the class instead of surrounding it with a try/catch, but the exact same thing happens. I have no idea what I'm doing wrong. For additional info, here is my current file structure (files are copied over from their original destinations, not linked): enter image description here

I can provide additional info as needed.

  • 1
    The jar can't find the `images/New Trax Map Edited.png` file from the current execution context. For example if you `dir` from `C:\Users\benaa\Documents\School\2420\RouteFinder`, it MUST contain a `images` directory with the `New Trax Map Edited.png` file in it. I would, instead, recommend packing the image as an embedded resource, so it's included inside the Jar, then you would just use `img = ImageIO.read(getClass().getResource("/images/New Trax Map Edited.png"));` instead. I don't use Eclipse, so I can't help you with how embedded the resource would work, but it's been answered before – MadProgrammer Apr 29 '22 at 01:59
  • For [example](https://stackoverflow.com/questions/3721706/embedding-resources-images-sound-bits-etc-into-a-java-project-then-use-those) – MadProgrammer Apr 29 '22 at 02:00
  • What does your `getClass().getResource()` call look like? – nitind Apr 29 '22 at 04:01
  • You need to get the paths correct: Better to load your files/images as resources: https://technojeeves.com/index.php/aliasjava1/80-loading-files-as-resources-in-java-with-eclipse – g00se Apr 29 '22 at 09:36

0 Answers0