1

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()
Community
  • 1
  • 1
Adam
  • 11
  • 2
  • Please provide code, it will help immensely. – loafy Feb 17 '15 at 02:54
  • I just updated the help request with the code I'm using. – Adam Feb 18 '15 at 00:09
  • try inserting a / before "audioplayer" when defining the path of the file. – loafy Feb 18 '15 at 04:18
  • I added a catch for when my_audio=null to avoid nullpointer exceptions and see them as something more useful. Attempting to put a '/' before "audioplayer" when defining the path of the file throws an IOException (before my code modification it would have thrown a NullPointerException). – Adam Feb 18 '15 at 22:32
  • Hmm. That's odd. Have you tried double backslashes? - \\ – loafy Feb 19 '15 at 16:55
  • Using double backslashes yields the same problem of an IOException being thrown. This occurs if I have leading double backslashes or no leading double backslashes. If I use forward slashes, I cannot have any leading forward slashes or the program fails during debug. – Adam Feb 19 '15 at 21:32
  • Is the problem that I'm currently trying to execute the .jar from the command line? – Adam Feb 19 '15 at 22:48
  • Possibly. Is your program meant to be run as a GUI, or a command line program? If it's a GUI, just export it as a runnable JAR file and then run it by double clicking, or converting to an EXE. If it's just meant to be run from the command prompt, then there shouldn't be an issue on how you are running it. – loafy Feb 20 '15 at 02:45
  • There's no GUI implemented atm for the program. It's runnable via the command line which is how I'm currently trying to execute it. It's in the command line where it's throwing the IOException that isn't thrown during the debugging process in Netbeans. – Adam Feb 20 '15 at 21:25
  • Why are you using ClipPlayer's getClassLoader(), try just using this.getClass().getResourceAsStream("path"); – loafy Feb 20 '15 at 23:11
  • @Loafayyy I saw it in another article. I made the change and I still get the same problem. In the JAR archive I have /Resources/Music/sound.wav inside the audioplayer package and I want to play background.wav. When I'm passing the /Resources/Music/background.wav to getResourceAsStream() do I need to put something infront of the /Resources? Like do I need to put //Resources? I tried ./Resources and that just gave another IOException (could not find packaged file) during the debug process. – Adam Feb 22 '15 at 01:43
  • I've added a GUI to see if it was just the commandline interface that was breaking audio playback. It wasn't and it still seems that the problem is that the program cannot locate the file archived in the JAR. I'm stumped. – Adam Feb 23 '15 at 22:32

0 Answers0