Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I put my resources for a Java program?

I'm programming in Java using Eclipse.

I have recently tried playing a sound file in my program. I did this:

URL url = this.getClass().getResource("ih.wav");
audioIn = AudioSystem.getAudioInputStream(url);
clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();

This only worked, when I tried to put ih.wav in the bin folder, as if the bin folder was the "base folder" for my project. Putting the file in the main folder of the project, didn't work. Putting it in the src folder, didn't work too.

Can someone explain to me where to put my resources for Java programs? Also, does it matter if I import the resources into Eclipse? Thanks

EDIT:

Tried to create a resources folder in the main project folder, still gives me a NullPointerException:

        URL url1 = this.getClass().getResource("res/ah.wav");
        audioIn = AudioSystem.getAudioInputStream(url1);
        clip1 = AudioSystem.getClip();
        clip1.open(audioIn);

        URL url2 = this.getClass().getResource("res/eh.wav");
        audioIn = AudioSystem.getAudioInputStream(url2);
        clip2 = AudioSystem.getClip();
        clip2.open(audioIn);

        URL url3 = this.getClass().getResource("res/ih.wav");
        audioIn = AudioSystem.getAudioInputStream(url3);
        clip3 = AudioSystem.getClip();
        clip3.open(audioIn);

        clip1.start();
        clip2.start();
        clip3.start();
like image 456
user3150201 Avatar asked Dec 12 '22 08:12

user3150201


1 Answers

Caveat: I don't use Eclipse, I've never used Eclipse, my knowledge of Eclipse amounts to reading other posts and answering questions here

Under your project, create a directory called resources. In here place all you "resources" you want to access from within your application. These will bundled with your application as embedded resources.

From within your application use either

this.getClass().getResource("/ah.wav")

or

this.getClass().getResource("/resources/ah.wav")

Assuming that you placed the files at the top level of the resources path. If they're in a sub-directory, you'll need to provide the full path...

Clean, build and test

like image 125
MadProgrammer Avatar answered Dec 23 '22 13:12

MadProgrammer