0

All I want is to get this into a Jar working, no problems in Eclipse, but jar does not play music. I tried several "solutions" from google and stackoverflow.

class Soundboard implements ActionListener {

    public Soundboard() {
    }

    @Override
    public void actionPerformed(ActionEvent ev) {
        if (ev.getSource() instanceof JButton) {
            JButton b = (JButton) ev.getSource();
            // getClass().getSystemResource("images/SomeImage.png")
            String build = "sound/" + b.getText() + ".au";
            try {
                playMusic(new File(build));
            } catch (UnsupportedAudioFileException | IOException
                    | LineUnavailableException e) {
                e.printStackTrace();
            }
        }
    }

    private void playMusic(File file) throws UnsupportedAudioFileException,
            IOException, LineUnavailableException {
        AudioInputStream stream = AudioSystem.getAudioInputStream(file);
        AudioFormat format = stream.getFormat();
        DataLine.Info info = new DataLine.Info(Clip.class, format);
        Clip clip = (Clip) AudioSystem.getLine(info);
        clip.open(stream);
        clip.start();
    }
}
Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55
  • Given the current information, no one can help you. – Maroun Nov 26 '14 at 21:12
  • 1
    What, exactly, is not working? –  Nov 26 '14 at 21:12
  • 1
    Probably you should use the `ClassLoader` to load the content of the file as an `InputStream`. Also check whether the audio file is in the jar or not. – Gábor Bakos Nov 26 '14 at 21:18
  • @jdv I seems that the .jar does not find the Soundfiles located in "sound/*.au" –  Nov 26 '14 at 21:18
  • @GáborBakos Where should I add the ClassLoader? Audiofiles are in the jar! –  Nov 26 '14 at 21:20
  • Those are "resources" as pointed out in the first answer, and how the JVM finds resources at runtime is going to be different when they are in a jar. Possible duplicate: http://stackoverflow.com/questions/2171672/java-jar-file-cannot-find-resources –  Nov 26 '14 at 21:21
  • Or, even better: http://stackoverflow.com/questions/2393194/how-to-access-resources-in-jar-file –  Nov 26 '14 at 21:28
  • @jdv getRessource() does not work on audio/images... ClassLoader was that what I was looking for! –  Nov 26 '14 at 21:46

2 Answers2

0

I suspect that when you are creating your jar, the resource files are being put into in a different directory to where they are when you build in Eclipse, so your code is looking in the wrong place. You can see where your files are by examining the jar directory structure with jar -tvf myFile.jar

Have a research on where to put resource files in a jar so that your java code can find them.

Hope that helps.

Edit: What do you see when you execute the jar command above? You should see something like

     >jar -tvf soap-tcp.jar
     0 Thu Aug 21 05:05:26 BST 2014 META-INF/
  5118 Thu Aug 21 05:05:24 BST 2014 META-INF/MANIFEST.MF
     0 Thu Aug 21 05:05:22 BST 2014 META-INF/logmessages/
     0 Thu Aug 21 05:05:22 BST 2014 META-INF/loggerinfo/
     0 Thu Aug 21 05:05:22 BST 2014 org/
     0 Thu Aug 21 05:05:22 BST 2014 org/glassfish/
     0 Thu Aug 21 05:05:22 BST 2014 org/glassfish/webservices/
     0 Thu Aug 21 05:05:22 BST 2014 org/glassfish/webservices/transport/

only with your directories and files inside the jar. That will tell you where your packaging process has put your audio files.

user384842
  • 1,946
  • 3
  • 17
  • 24
  • File structur is: - sound - images - (package) –  Nov 26 '14 at 21:22
  • I'm on Windows, opened it with WinRAR and It looks similar to yours! Problem was ClassLoader... –  Nov 26 '14 at 21:40
0

Probably you should use the ClassLoader to load the content of the file as an InputStream. Also check whether the audio file is in the jar or not. – @Gábor Bakos 8 mins ago

It's working! ClassLoader was what was missing!

AudioInputStream stream = AudioSystem.getAudioInputStream(ClassLoader.getSystemResource(file));

  • You should ask him to post it as an answer, then mark it as "accepted answer". An upvote never hurts either :) – Vince Nov 26 '14 at 22:02