Take the 2-minute tour ×

This is the first time I've implemented sounds but I can't figure out where to actually place the sounds to play them. I am using Eclipse as my IDE and I've put my sounds in a folder called sounds.

The following code is what I've used to create one of the audioclip objects:

private final String background = "." + slash + "sounds" + slash + "background.wav";

    try {
        backgroundClip = Applet.newAudioClip(new File(background).toURI().toURL());
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block

I don't hear anything. When I remove the try/catch I get an error saying that it is unable to find the file. I placed my sound folder in both /src and /bin but neither can find it. where do I put it?

share|improve this question
1) String background = "." + slash + .. If the value of slash is / it is wrong. If the value is System.getProperty("file.separator") it should be called separator (or something similar). But File has constructors that can do all that for you. 2) Since Java 1.3, the J2SE has offered that javax.sound.sampled API. Use that instead of an AudioClip. –  Andrew Thompson Dec 13 '11 at 3:44
add comment

3 Answers

up vote 1 down vote accepted

The base for your project is the level above the src folder. So using . will put you at your project folder.


. = project_root

./src = default_package
./src/packagename = inside the package named "packagename"

./sounds/background.wav = a .wav file in the sounds folder, in the project_root

Using your current path, you need to put your .wav file in the sounds folder in the project_root.

The path will end up being project_root/sounds/background.wav.

share|improve this answer
What? isn't the "." refers to the current folder where the execution is going on? –  Naved Dec 13 '11 at 3:34
yes. The execution occurs at project_base. I've spent days trying to figure this out before, and these were my results. –  Jon Dec 13 '11 at 3:36
add comment

Have you tried just using the absolute path to the file's location?

share|improve this answer
add comment

See this thread to understand why application resources should be obtained by URL obtained from getResource(), rather than a File converted to an URL.

share|improve this answer
Concur. And with Java 7, it's even more important to use URL in your getResource(). I ran into a similar problem here: stackoverflow.com/questions/8091967/… –  Phil Freihofner Feb 9 '12 at 4:34
add comment

Your Answer


