2

I'm trying to prank my friends with the semester's final Java program. It's a version of Space Invaders, and I added some annoying mp3 sounds to play after the game closes. I used Javazoom to play the mp3, and everything worked before exporting the project.

I learned resource files needed to be kept in a resources folder, which I have made. mp3 files and png files were placed in the same folder, and using code like the following works fine to display the images.

Image bg = Toolkit.getDefaultToolkit().getImage(getClass().getResource("./resources/bg2.png"));

However, the mp3 player cannot seem to access the resources folder. The InputStream always comes back as null when using this code:

//where fileName = /C:/Users/myName/JavaF16/Personal/bin/SpaceInvaders/resources/6.mp3
InputStream is = SpaceInvaders.class.getResourceAsStream(fileName.toString());
Player playMP3 = new Player(is);

fileName is a valid location, I can paste it in my file explorer and the mp3 will play. I read in that it needed a / on the front, but using that had no effect. I will admit that I'm not sure the project's folders are arranged properly, our class never taught about build paths or even source folders. But I did add all the resources to the build path. Here's a snapshot of the folder arrangement.

If I look at the exported jar in winRAR, all of the resources are included.

Folder Arrangement

Any help would be much appreciated, thank you.

approxiblue
  • 6,982
  • 16
  • 51
  • 59
Mary
  • 29
  • 3
  • 1
    Did you try `SpaceInvaders.class.getResourceAsStream("./resources/6.mp3");`, like the image path? – approxiblue Dec 19 '16 at 15:37
  • 1
    I had tried doing that with getResource, but not after switching to getResourceAsStream. It worked, thank you! – Mary Dec 19 '16 at 16:07
  • Possible duplicate of [getResourceAsStream returns null](http://stackoverflow.com/questions/16570523/getresourceasstream-returns-null) – approxiblue Dec 19 '16 at 16:18

0 Answers0