开发者

Including all libraries, audio, etc. of a Java program in one JAR file

I am using Netbeans and I am trying to figure out how I can put all of my libraries, music, images, etc. in one JAR file for distribution. I think I have the libraries figured out, but the audio, images, and other such files are giving me trouble.

In my current project I have an audio file that I want to embed in the JAR file too. First I tried one-jar but after a couple of hours I gave up on it. I put the audio file into the JAR file just fine, but I cannot access it from my program. I know I need to use getResourceAsStream as suggested here but I am unclear what I do after I get the input stream. The only way I can see to make it work is to use use the InputStream and create a whole new file (seen below... and it works), but creating a new file seems like a waste (and I don't want people to see an audio file appear when my program is running). Is there no w开发者_运维技巧ay to directly access the audio file while it is still contained in the .JAR file?

File file = new File("myAudio.wav");
InputStream stream = mypackage.MyApp.class.getResourceAsStream("audio/myAudio.wav");
        try {
            OutputStream out = new FileOutputStream(file);
            byte buf[] = new byte[1024];
            int len;
            while ((len = inputStream.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.close();
            inputStream.close();
        } catch (IOException e) {
        }

EDIT:

The internal structure of my JAR file Contains 1.) a library package (Jama), 2.) my package which is the direct parent of my class files and a folder called "audio" which contains myAudio.wav, and 3.) a META-INF folder which contains my manifest.mf.

EDIT

Audio stream is read something like this. I have tried to use the InputStream directly but I have not had success. I want to point out again that I already have it when I create a new audio file from the input stream of the audio file contained JAR file, but like I said before, it seems like a waste to create a big audio file every time a program is run when the file already exists in the JAR. This file recreation is what I am trying to avoid.

AudioInputStream stream;
Clip music;
        try {
            stream = AudioSystem.getAudioInputStream(file);
        } catch (IOException e) {
        } catch (UnsupportedAudioFileException e) {
        }
        try {
            music = AudioSystem.getClip();
        } catch (LineUnavailableException e) {
        }
        try {
            start();
        } catch (Exception e) {
        }
public void start() throws Exception {
    music.open((AudioInputStream) stream);
    music.start();
    music.loop(Clip.LOOP_CONTINUOUSLY);
}


Doesn't AudioSystem.getAudioInputStream(stream); work for you?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜