Take the 2-minute tour ×

I have found plenty of threads on this subject but i still dont get it to work. It works easily from the compiler but not from the jar file. It seems the jar file finds the audio file but it just doest play it.

import sun.audio.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class JarSoundTest1 extends JFrame { 
  JButton button;
  InputStream in;
  AudioStream as;

  public JarSoundTest1() throws Exception {

      JOptionPane.showMessageDialog(null, this.getClass().getResource("blopp.wav"));          

      button = new JButton("Click to Blopp!");
      button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
                in = this.getClass().getResourceAsStream("blopp.wav");
                as = new AudioStream (in);

                JOptionPane.showMessageDialog(null, "try");

            }catch(Exception ex){

                JOptionPane.showMessageDialog(null, "catch");               

      JFrame frame = new JarSoundTest1();
      frame.setSize(200, 200);

The JOptionPane displays "try" every time the button is clicked, indicating the file is found i assume? Still no sound is played. I have tried using audio files placed both inside and outside the jar file. Help to understand this is much appreciated.

share|improve this question
Note, it is probably better to use javax.sound.sampled than the sun ones because there is no guarantee of correctness over time with these implementations. –  demongolem Feb 4 at 18:33
add comment

1 Answer

Ok, I got it finally. I don't think it is a sound issue I am quite sure as I can get a wav to play with code very similar to yours, it is a resource issue within jars which can be quite tricky. I would suggest the following based upon experiments in editing the above code:

getClass vs getClassLoader

If you use getClass, the path must begin with the forward slash, /. So, if your .wav is at the top level, ./blopp.wav is the way to go. So likely that is the only adjustment you will need if the .wav file is recognized by sun.audio.*.

If you can find the resource in the jar, but cannot hear audio, maybe try a different .wav file. Some good wav files are here

share|improve this answer
add comment

Your Answer


