I'm using Netbeans and I'm just trying to write a simple program (for practice purposes) that streams a file packaged into a compiled .JAR. In this case the packaged file is an audio clip.
I've cleaned and built my project and included the file I want to stream in the /src/package/MyResources/ folder and the InputStream points to the appropriate location as well.
When I run a debug or "Run Project" in Netbeans the stream works fine and the packaged resource is found. Consequentially the audio file plays.
When I try to run the compiled .JAR from the command prompt using "java -jar MyJar.jar" the packaged resource can no longer be found and an IOException is thrown. Opening the .jar with an archiving program shows that the resource file IS present with the same folder structure as defined before compilation.
I'm not sure if some code is needed? But I don't understand why the packaged resource cannot be accessed.
EDIT: So the current code applies a lot of what was given by a helpful user here:Trouble playing wav in Java
package audioplayer;
import javax.sound.sampled.*;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import javax.sound.sampled.LineEvent.Type;
public class ClipPlayer
{
public static void play()throws UnsupportedAudioFileException, LineUnavailableException, InterruptedException, FileNotFoundException
{
class AudioListener implements LineListener//Sub class that can be used to ensure file completion.
{
//AudioListener ensures that the streamed file finishes streaming (or playing in our case) before the thread can be stopped.
private boolean done=false;
@Override
public synchronized void update(LineEvent event)
{
Type eventType = event.getType();
if (eventType == Type.STOP || eventType == Type.CLOSE)
{
done = true;
notifyAll();
}
}
public synchronized void waitUntilDone() throws InterruptedException
{
while(!done)
{
wait();
}
}
}
try
{
InputStream my_audio=ClipPlayer.class.getClassLoader().getResourceAsStream("Resources/Music/background.wav");
if(my_audio!=null)
{
//Since we are embedding the audio file in the class, we do not need to create a file object.
AudioListener bg_listener = new AudioListener();
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(my_audio);//We create a player with the indicated resource.
try
{
Clip clip = AudioSystem.getClip();//The audio system has the file ready to stream now, we capture that stream.
clip.addLineListener(bg_listener);//Then we add the audiolistener so we ensure completion if something tells the thread to stop.
clip.open(audioInputStream);//Open the stream for playing, we're almost there now.
try
{
clip.start();//Start playing the audio stream. Note this creates a seperate thread.
bg_listener.waitUntilDone();//So we can invoke the listener while the clip is playing.
}
finally //A finally statement is used to ensure the resource stream is properly closed even if an error occurs.
{
clip.close();
}
}
finally //We do the same for the AudioSystem too.
{
audioInputStream.close();
}
}
else
{
throw new FileNotFoundException("File not found.");
}
}
catch(IOException dir)
{
System.err.println("Could not get packaged resource.");
}
}
}
So when I debug or run in Netbeans, I get no error. When I try to run the program through the commandline however with java -jar MyClass.jar I'm told "Could not get packaged resource."
MyClass.jar contains a line (with try/catch) that is:
ClipPlayer.play()