Get Off My Lawn
Get Off My Lawn

Reputation: 36299

Loading image outside of project

I want to have a project that has some folders next to my jar file:

+- MyProject.jar
+- graphics
    +- image1.jpg
    +- image2.jpg
    +- paper.jpg
+- plugins
    +- plugin1.jar
    +- plugin2.jar
    +- plugin3.jar

Within MyProject.jar I have this line:

URL loc = this.getClass().getResource("/../graphics/paper.jpg");

which is triggering this error:

Exception in thread "Timer-0" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(ImageIO.java:1388)
    at fotofilter.filters.Blueprint.getPaper(Blueprint.java:116)
    at fotofilter.filters.Blueprint.filter(Blueprint.java:45)
    at fotofilter.FotoFilter.blueprint(FotoFilter.java:103)
    at fotofilter.FotoFilter$1.run(FotoFilter.java:55)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)

Am I using this properly?

Upvotes: 1

Views: 498

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

getResource is used to find resources within the context of the current Classpath. Normally, . isn't part of the Classpath, this means that the class loader won't try and look in the graphics directory

getResource won't be able to find these resources, unless, . is part of the class path.

getResource will prefix each Classpath element to the path, this means, unless . is part of the Classpath, getResource will not work. In this case, you should try using File instead

Upvotes: 1

Related Questions