在Eclipse的可运行jar文件中包含图片和声音文件 [英] Including pictures and sound files in a runnable jar file from Eclipse

查看:518
本文介绍了在Eclipse的可运行jar文件中包含图片和声音文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Eclipse编写的口袋妖怪游戏中使用了很多声音和图片文件。一切都在Eclipse中完美运行,但是当我将其导出为可运行的jar时,我得到一个错误,当我尝试运行它时媒体不可用。

I have many sound and picture files I am using in a pokemon game I'm writing with Eclipse. Everything runs perfectly in Eclipse, but when I export it as a runnable jar, I get an error that the media is unavailable when I try running it.

这是我用来播放mp3文件的代码:

This is the code I'm using for playing an mp3 file:

    File file = new File("src/pokemon/mp3s/opening.mp3");
    final String mediaLocation = file.toURI().toURL().toExternalForm();
    Media media = new Media(mediaLocation);
    mp = new MediaPlayer(media);

导出为可运行的jar后,我用java -jar Pokemon2.jar执行它并得到此错误消息:

After exporting as a runnable jar, I execute it with java -jar Pokemon2.jar and get this error message:

线程中的异常MainMediaException:MEDIA_UNAVAILABLE:C:\ Users \Nick \Desktop\src\pokemon\mp3s\opening .mp3
系统找不到指定的路径

Exception in thread "Main" MediaException: MEDIA_UNAVAILABLE : C:\Users\Nick\Desktop\src\pokemon\mp3s\opening.mp3 The system cannot find the path specified

如何让我的runnable jar能够找到这些额外的资源?

How do I make my runnable jar be able to locate these additional resources?

推荐答案

已部署的jar文件不包含源文件或src文件夹。
您的IDE可以编译源文件并将它们放在其他位置(例如,在build \classes中的netbeans中)。当您部署项目时,您的mp3文件将出现在该jar文件中,而不是 src 文件夹中。

Deployed jar file doesn't contains source files or src folder. It is your IDE which make source files compiled and put them at other place, (e.g. in netbeans inside build\classes). And when you deploy your project it is your mp3 file will be present inside that jar file, not in src folder.

你应该尝试另一种方法。

假设你的文件存在于 src / pokemon / mp3s / opening.mp3

并且一个类存在于 src / pokemon / Referer.java

You should try another approach.
suppose your file is present in src/pokemon/mp3s/opening.mp3
and a class is present in src/pokemon/Referer.java

现在您可以访问 mp3 文件了以 Referer 类作为参考:

Now you can access your mp3 file by taking Referer class as reference :

URL fileUrl = Referer.class.getResource("mp3s/opening.mp3");

这篇关于在Eclipse的可运行jar文件中包含图片和声音文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆