Music not working in compiled .jar

Hi everyone!
That has been a few months I’m following this website’s new projects. I had never registered before because I’m French, and not that good at English. But I have now a problem with my latest project. And, I’ve seen someone’s post (can’t remember whose, sorry) who had the same problem as mine with English ;D So I registered to ask my question :wink:

I’ve been coding a small game to practise with Java (I’m quite new to it) and I’m now having troubles making the .jar… See, the sounds, which work perfectly when I run the code (I’m running it with cmd since I do not like IDEs), do not function at all after compiling. It seems to be a reccurent problem, but none of the solutions I found on the net helped me fix it :’(

Ok, now to it. Here is my code I use to run the sounds (I simply call the play() method) :

public static void play(int i){
		new Thread(new Runnable()
		{
			SourceDataLine soundLine;
			public void run()
			{
				soundLine = null;
				int BUFFER_SIZE = 64*1024;
	
				try {
					AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(this.getClass().getResource("Music\\sound" + i + ".wav"));
					AudioFormat audioFormat = audioInputStream.getFormat();
					DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
					soundLine = (SourceDataLine) AudioSystem.getLine(info);
					soundLine.open(audioFormat);
					soundLine.start();
					int nBytesRead = 0;
					byte[] sampledData = new byte[BUFFER_SIZE];
					while (nBytesRead != -1){
						nBytesRead = audioInputStream.read(sampledData, 0, sampledData.length);
						if (nBytesRead >= 0){
							soundLine.write(sampledData, 0, nBytesRead);
						}		

					}
				} catch (Exception ex){
					ex.printStackTrace();
				}finally{
					soundLine.drain();
					soundLine.close();
				}
			}
		}).start();
	}

And this is the error I get when executing the jar using java -jar prog.jar in cmd :

java.lang.NullPointerException
        at com.sun.media.sound.StandardMidiFileReader.getSequence(Unknown Source)
        at javax.sound.midi.MidiSystem.getSequence(Unknown Source)
        at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unknown Source)
        at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
        at Music$1.run(Music.java:15)
        at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-2" java.lang.NullPointerException
        at Music$1.run(Music.java:34)
        at java.lang.Thread.run(Unknown Source)

The file sound1.wav, for example, is in the correct repertory, as it runs when doing java Prog (where Prog.class is my main class)…

If someone could help me … ;D
Thank you very much indeed ! :slight_smile:
J0

Are you sure you have your sound files in your .jar file once you compile them?

And why are you not using IDE? They probably cut development time in half.

Hi!
First of all, thanks for your answer ;D
I’m not using an IDE because, sorry for those who are using one, I don’t like the idea of being ‘assisted’ if you see what I mean. In fact, I prefer understanding on my own how to do :smiley:

About the sound files which should be in the .jar, I’ve had a look, but there are no .wav nor .png (which work when I double-click the .jar). Only MANIFEST.MF and my .class…

Where should the .wav be in the .jar please ? :-
J0

I’m not sure how you should compile it by hand so that it is going to contain all your additional files but AFAIK, you can always add those later on to the JAR.
Also paths are a bit tricky when you store things inside a JAR, you should use [icode]getResource(“yourmusicfile.wav”);[/icode] or [icode]getResourceAsStream(“yourmusicfile.wav”);[/icode], for details see this link.
Honestly, I’m kind of against packaging everything into your JAR, I think having folders next to your JAR is an easier and better (but obviously a bit less portable) solution. :slight_smile:
By the way have you checked the line that the stacktrace points to? What is it? It could also help if you could copy your entire class to JGO’s pastebin and share it with us.

Also about the IDE thing: I would probably recommend VIM for almost all the languages out there, but for Java to avoid being error prone and be efficient you really do need a good IDE. Try out Eclipse, Netbeans and IntelliJ IDEA, those are the most used IDEs (and all of them are cross platform), I’m sure you’ll like at least one of them (especially because of their debug capabilities). :wink:

I just tried putting my sounds files in the .jar but still didn’t work :-X

This is my whole class which is supposed to play sounds:

import javax.sound.sampled.*;

class Music{
	public static void play(int i){
		new Thread(new Runnable()
		{
			SourceDataLine soundLine;
			public void run()
			{
				soundLine = null;
				int BUFFER_SIZE = 64*1024;
	
				// Set up the AudioInputStream
				try {
					AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(this.getClass().getResource("Music\\Sound" + i + ".wav"));
					AudioFormat audioFormat = audioInputStream.getFormat();
					DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
					soundLine = (SourceDataLine) AudioSystem.getLine(info);
					soundLine.open(audioFormat);
					soundLine.start();
					int nBytesRead = 0;
					byte[] sampledData = new byte[BUFFER_SIZE];
					while (nBytesRead != -1){
						nBytesRead = audioInputStream.read(sampledData, 0, sampledData.length);
						if (nBytesRead >= 0){
							//Audio data written in the Mixer
							soundLine.write(sampledData, 0, nBytesRead);
						}		

					}
				} catch (Exception ex){
					ex.printStackTrace();
				}finally{
					soundLine.drain();
					soundLine.close();
				}
			}
		}).start();
	}
}

I hope someone will find out what is going wrong with my .jar :clue:
Anyway, thanks a lot for trying to help me out 8)
J0

As long as you don’t need to get anything done, you probably can do things your own way… The way that feels best… If you are just spending time programming.

If you need to get something done however, you should probably get all the help you can get.

Yeah sure, but, see, this thing has been bugging me since thursday, and I have no idea about how to get this sound to work. I’ve tried all I could find, and whatever the method used is, or where I put my .wav files, the sound won’t play in compiled .jar…
When I finally managed to get the sounds when ran in cmd on wednesday (using sun.audio.* :persecutioncomplex:), I thought that it was the end of my problems with sounds. But it was just the beginning of it ???

I have a project of a game, and my music composer has already done awesome musics for it. But I’m not even starting the programming (gotta train a little before ;)), and I’m already having problems with it >:(

Well, thank you for any advice, no matter if it helps me or not (even though I’d greatly appreciate the one who’ll find out how to do ;D)
J0

Okay, got some news here.
After trying again and again, I’ve did another method to compile my project. Instead of doing the following in cmd

jar cvmf MANIFEST.MF prog.jar *.class

and then putting manually my sounds file in the .jar, I tried changing their place. First, they are no longer in the Music repertory, but directly at the project root. And I compiled (always in cmd) with :

jar cvmf MANIFEST.MF prog.jar *.class *.wav

See the difference ? I compiled the .wav files with the .class (don’t you ask me what’s different between this and adding them manually, I DON’T KNOW ???).
Anyway, now, it’s always the same when I double-click the jar (no sounds running) BUT when using

java -jar prog.jar

in cmd, the sounds work fine ! :o

I don’t understand a thing with those sounds ???
It would be incredibly greatly appreciated if some Java God could help me there :stuck_out_tongue:
Thanks for reading !
J0

Okay, it’s all nicely working now, thank you all ! ;D