将Java程序的所有库、音频等包含在一个JAR文件中

发布于 2024-11-03 12:16:15 字数 2115 浏览 0 评论 0原文

我正在使用 Netbeans,并且正在尝试弄清楚如何将所有库、音乐、图像等放入一个 JAR 文件中进行分发。我想我已经弄清楚了库,但是音频、图像和其他此类文件给我带来了麻烦。

在我当前的项目中,我有一个音频文件,我也想将其嵌入到 JAR 文件中。首先我尝试了 one-jar 但几个小时后我放弃了。我将音频文件放入 JAR 文件中,但我无法从程序中访问它。我知道我需要按照建议使用 getResourceAsStream 这里 但我不清楚在获得输入流后我要做什么。我能看到让它工作的唯一方法是使用InputStream并创建一个全新的文件(如下所示......并且它有效),但创建一个新文件似乎浪费(而且我不希望人们在我的程序运行时看到音频文件)。 当音频文件仍然包含在 .JAR 文件中时,是否无法直接访问该音频文件?

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) {
        }

编辑:

我的 JAR 的内部结构文件包含 1.) 一个库包 (Jama),2.) 我的包是我的类文件的直接父级和一个名为“audio”的文件夹,其中包含 myAudio.wav,以及 3.) 一个包含我的清单的 META-INF 文件夹。 MF.

编辑

音频流的读取方式如下。我尝试直接使用 InputStream 但没有成功。我想再次指出,当我从包含 JAR 文件的音频文件的输入流创建一个新的音频文件时,我已经有了它,但就像我之前说过的,创建它似乎是一种浪费当文件已存在于 JAR 中时,每次运行程序时都会生成一个大音频文件。我试图避免这种文件重新创建。

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);
}

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 way 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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〃温暖了心ぐ 2024-11-10 12:16:15

AudioSystem.getAudioInputStream(stream); 不适合您吗?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文