如何在Eclipse中将所有资源打包成可运行的JAR

发布于 2024-11-24 14:34:50 字数 541 浏览 2 评论 0原文

我目前正在用 Java 开发自己的小视频游戏,但截至目前,当我将 Runnable Jar 文件提供给某人以便他们可以玩时,我必须向他们发送 22 个其他所需的资源文件,并让他们放置它在同一个文件夹中。这非常不方便,而且我知道有一种方法可以自动将这些资源打包到可运行的 jar 文件中。如果我不能这样做,那么我至少想知道如何访问 Runnable Jar 中的文件,这样我就可以简单地复制粘贴其中的文件。我查遍了互联网,没有任何建议有效。如果我使用以下代码,我可以在 Eclipse 中正确运行该程序:

    URL url = this.getClass().getResource("Jugger-Nog.jpg");
         image = ImageIO.read(new File(filePath));
         //image = ImageIO.read(url);

第一行创建一个 url,这是我在许多网页上读到的,但使用它和第三行代码在 JAR 或 Eclipse 中不起作用,但第二行本身在 Eclipse 中就可以正常工作。任何想法,因为互联网上没有很多。

I'm currently working on my own little video game in Java, but as of now when i give the Runnable Jar file to someone so they can play, i have to send them 22 other resource files which are required, and have them place it in the same folder. This is unbelievably inconvenient and i know that there is a way to automatically package these resources into the runnable jar file. If i cant do this then i would like to at least know how to access the files in the Runnable Jar so i could simply copy paste the files inside. I have combed the internet and no suggestions have worked. I can run the program properly in eclipse if i use this code:

    URL url = this.getClass().getResource("Jugger-Nog.jpg");
         image = ImageIO.read(new File(filePath));
         //image = ImageIO.read(url);

The first line creates a url, which is what i read on many web pages, but using that and the 3rd line of code did not work in the JAR or in Eclipse, but the second line on its own will work just fine in Eclipse. Any ideas, because the internet doesnt have many.

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

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

发布评论

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

评论(4

爱人如己 2024-12-01 14:34:50

将 JAR 文件和资源文件放在同一目录中而不是将其全部打包到 JAR 中是不是很糟糕?

您可以将游戏打包到安装程序中,将其全部复制到程序文件夹中并放置一些快捷方式。

Is it so bad put the JAR file and the resource files just into the same directory, rather than package all of it inside the JAR?

You could package your game on an Installer which copies all of it to the Program's folder and puts some shortcuts.

慈悲佛祖 2024-12-01 14:34:50

我也遇到了这个问题,但我的解决方案是程序检查 %app data%/roaming/ 中调用 myApp 的文件夹是否存在。如果没有,程序将执行 mkdir 并创建该文件夹。然后就是让程序知道所有源文件所在的位置。无需费力地处理您的文件。

I had this problem too, but my solution was that the program checks if a folder in %app data%/roaming/ called for myApp exists. If it doesn't the program does a mkdir and makes that folder. Then it's just to let the program know the location of where all the source files is located. No need to hockey pokey around with your files.

三月梨花 2024-12-01 14:34:50

你应该使用:

InputStream in = this.getClass().getResourceAsStream("Jugger-Nog.jpg");
image = ImageIO.read(in);

You should use:

InputStream in = this.getClass().getResourceAsStream("Jugger-Nog.jpg");
image = ImageIO.read(in);
醉生梦死 2024-12-01 14:34:50

设置资源文件夹

您需要做的第一件事是设置资源文件夹。在 Eclipse 中,右键单击您的项目并导航到“新建”>“文件夹”,随意命名该文件夹。再次右键单击您的项目并导航到“属性”。在此窗口中,单击“Java 构建路径”,然后单击“库”选项卡。单击“添加类文件夹”并选择您之前创建的文件夹。这是您可以放置​​资源文件的地方。

访问您的资源

在您的 Java 代码中,您可以使用以下内容:

%CLASSNAME%.class.getResourceAsStream("/%FILENAME%");

其中 %CLASSNAME% 是您当前所在类的名称,%FILENAME% 是该类的名称。您想要访问的文件。这将返回一个InputStream。如果您希望加载图像,您可以简单地通过调用 ImageIO.read(stream) 来包装它,例如:

ImageIO.read(%CLASSNAME%.class.getResourceAsStream("/%FILENAME%"));

另外,请注意您要访问的文件前面的斜杠,它是强制性的我耽心。

为什么我们不能使用文件

你不能使用File,因为它代表文件系统上的实际物理文件,当你将其打包到 jar 中时,它是 jar 文件的一部分,而不是文件系统上的实际文件。

Setup Resource Folder

The first thing you need to do is setup a resource folder. In Eclipse, right-click on your project and navigate to New>Folder, call this folder whatever you want. Right-click on your project again and navigate to Properties. In this window click on Java Build Path and then on the Libraries tab. Click Add Class Folder and select the folder you created earlier. This is where you can put your resource files.

Accessing Your Resources

In your Java code you can use the following:

%CLASSNAME%.class.getResourceAsStream("/%FILENAME%");

Where %CLASSNAME% is the name of the class you are currently in and %FILENAME% is the name of the file you wish to access. This will return an InputStream. If you wish to load an image you can simply wrap it with a call to ImageIO.read(stream) for example:

ImageIO.read(%CLASSNAME%.class.getResourceAsStream("/%FILENAME%"));

Also, notice the slash the comes before the file you want to access, it is mandatory I'm afraid.

Why Can't We Use File

You can 't use a File because it represents an actual physical file on your file system, when you package it into a jar it is a part of your jar file and not an actual file on your file system.

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