-1

I'm having an odd problem. I have a game of sorts where an image bounces around the screen in 'darkness' (black Rect) and the mouse cursor represents a flashlight shining through the dark (subtracts an oval).

Now I have one .png image and one .wav sound file. I have them both in a "resources" folder in my project folder and I access them via the filepaths "resources/myImage.png" and "resources/mySound.wav".

Everything works fine when I run the program in Eclipse (using Kepler btw); the image shows up fine, and the sound plays when commanded to, but as soon as I export it as a jar and run, it shows the program name for a few seconds as if it were an active program, but shortly after quits/crashes. I open Terminal and run java -jar /Users/MyUsername/Desktop/MyProg.jar which garners the same result but I get by crash log in Terminal. It reads:

Aug 23, 2013 4:10:02 PM com.spotlight.TestPane <init>
SEVERE: null
javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)
    at com.spotlight.TestPane.<init>(TestPane.java:62)
    at com.spotlight.Spotlight$1.run(Spotlight.java:32)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.spotlight.TestPane.<init>(TestPane.java:69)
    at com.spotlight.Spotlight$1.run(Spotlight.java:32)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.spotlight.TestPane.move(TestPane.java:169)
    at com.spotlight.TestPane$1.actionPerformed(TestPane.java:51)
    at javax.swing.Timer.fireActionPerformed(Timer.java:312)
    at javax.swing.Timer$DoPostEvent.run(Timer.java:244)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

For refence, my code for reading the image file is:

BufferedImage img;
//...
try {
        img = ImageIO.read(new File("resources/myImage.png"));
    } catch (IOException ex) {
        Logger.getLogger(Spotlight.class.getName()).log(Level.SEVERE, null, ex);
    }

The way I exported my program is as follows:

  • I right-click on my project folder and select "Export"
  • I select the "Runnable Jar" option
  • I select the correct Launch Configuration
  • I browse to the correct export destination (/Users/MyUsername/Desktop/MyProg.jar)
  • Under "Library Handling," I choose "Extract required libraries into generated JAR"
  • "Save as ANT script" remains unchecked
  • I click "Finish," and the jar generates without any warnings

I know SO isn't really for posting crash logs, but I'm sorta stumped here, so any help is appreciated.


UPDATE: I added a System.err.println statement in the try/catch block, and used Unarchiver to extract the jar's contents and the resources folder doesn't appear. If the resources aren't exporting, that might be my problem, but I don't know how to fix it.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
ImagineBaggins
  • 135
  • 1
  • 9
  • 1
    By the time of deployment, those resources will likely become an [tag:embedded-resource]. That being the case, the resource must be accessed by `URL` instead of `File`. See the [info page](http://stackoverflow.com/tags/embedded-resource/info) for the tag, for a way to form an `URL`. – Andrew Thompson Aug 23 '13 at 21:46
  • http://stackoverflow.com/questions/18093092/xml-filenotfoundexception-using-slick2d-library-in-java/18093227#18093227 – Luca Basso Ricci Aug 23 '13 at 21:50
  • Thanks for the info, but I'm sort of a noob at Java and need some more help. I now have `URL imageURL;` and which is later initialized as `imageURL = this.getClass().getResource("resources/images/myImage.png");` My media is stored in a source folder "resources" which has 2 folders within it named "images" and "sound". When I try to run the program, it doesn't seem to be able to locate the files, returning a `java.lang.IllegalArgumentException: input == null` and the error comes from the line where I have `img = ImageIO.read(imageURL);`. Sorry for the wall of text, but any ideas? – ImagineBaggins Aug 23 '13 at 22:29
  • 1
    Tip: 1) Add @bellabax (or whoever - the `@` is important) to *notify* them of a new message. 2) Change the string from `"resources/images/myImage.png"` (means 'relative to class package') to `"/resources/images/myImage.png"` (means - relative to 'root' of class-path). – Andrew Thompson Aug 24 '13 at 19:12

1 Answers1

1

Common problem commonly posted here. You're trying to access a resource as a file where no file exists. Stop. Jar files don't hold files, so don't try to access anything in them as files. Get the data as a resource instead. Google the issue because as I said, it is posted here several times a week.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373