Where should I put my resources for a Java program?

14,276

Solution 1

Best practice would be to create a folder (depending on your package structure) that is called either res or resources where you will keep project related resources such as any images or sound files.

Depending on preference, either put this folder in your main project folder MyProject/res or put it inside the relevant package, e.g. main.java.res

EDIT

To adhere more to your question, I'm not too familiar with the getResources() method, so instead I would personally recommend using some code similar to:

File file = new File("ih.wav");
audioIn = AudioSystem.getAudioInputStream(file);
clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();

That should produce the results you are looking for, given that you change the path of he file accordingly (e.g. "main/resources/ih.wav" or wherever you are storing the file.

Solution 2

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

Solution 3

I'd create a resources folder inside src something like src/main/resources. You would like to take a look to Maven that is project management and comprehension tool that help a lot in project organization.

Share:
14,276

Related videos on Youtube

user3150201
Author by

user3150201

Updated on September 14, 2022

Comments

  • user3150201
    user3150201 over 1 year

    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();
    
    • MadProgrammer
      MadProgrammer
      @nachokk Please make it answer so I can upvote it ;)
    • nachokk
      nachokk
      create a resources folder like -- src -- main `-- resources
  • user3150201
    user3150201 over 10 years
    I did exactly that, but for some reason I get a NullPointerException when trying to reach the files :\ Any ideas?
  • nachokk
    nachokk over 10 years
    @user3150201 Well it's always try and error, but see this answer if it helps you
  • nachokk
    nachokk over 10 years
    @user3150201 also make a clean & build