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