I'm relatively new to java, so please bear with me. Anyway, I've written a program that makes use of both images and audio, and I'm trying to package these into the .jar file. My goal is to be able to run the program without requiring any files besides the jar. Ive found that I can just add the images to the root of the jar using 7zip, but this doesn't work with the audio.

I'm using the jar and class found at http://www.cs.princeton.edu/introcs/faq/mp3/mp3.html to play mp3 files and reduce file size. It seems that this method doesn't work with files packaged into the jar I run. What can I do? Is setting a relative classpath likely to help? If it is, how do I use it? If I can't manage to package the audio, is there another (simple) way to play mp3 files (or some other compressed format) in a java program that would work?


6 Years
Discussion Span
Last Post by fregley

Thanks for the response, but the article didn't help. Using eclipse, I've already managed to extract all needed jars into my final one. I think the problem lies in this. When I ran the project from eclipse, I needed to put the images in the bin directory but the audio went in the project root. I think that the audio player is looking for the audio in a folder above the jar file/bin directory. How can I change this behavior or add a directory above the main jar?


Have you tried to do drag&drop these files into src/ of the Project Explorer (Eclipse) and then select "copy"?

Edited by martin5211: n/a


That's a really cool tip! I didn't know you could do that, but it doesn't work for the audio. It worked great for the images, though. When exporting, eclipse just put the new files in the root of the jar, and I think the audio has to be above the directory of the classes.

Currently, the folder structure looks like "Export.jar>classes and stuff", but I think it has to look like "Export.jar>audio files>classes and stuff", if that makes any sense at all.


OK, I've learned where the problem is coming from. The class I'm using to play the audio uses a FileInputStream, which apparently doesn't read files from inside a jar. I found this http://stackoverflow.com/questions/1913565/how-can-i-access-a-txt-file-in-a-jar-with-fileinputstream, but I can't seem to get the class provided to work. Any ideas on how to fix this?

For reference, the relevant code is this (filename is a string)

FileInputStream fis     = new FileInputStream(filename);
            BufferedInputStream bis = new BufferedInputStream(fis);
            player = new Player(bis);

Thank you very much! Now it works perfectly!

Unfortunately, now the program is rather jerky. Is there a way to handle to resourceasstream in a separate thread?

For anyone searching the internet for answers, the relevant code now looks like this:

InputStream fis = Omniverse.class.getResourceAsStream(filename);
        		BufferedInputStream bis = new BufferedInputStream(fis);
        		player = new Player(bis);
This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.