简体   繁体   English

如何将 package 的所有资源放入 Eclipse 中的可运行 JAR

[英]How to package all resources into runnable JAR in 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.我目前正在 Java 中开发我自己的小视频游戏,但是到目前为止,当我将 Runnable Jar 文件提供给某人以便他们可以玩时,我必须向他们发送 22 个其他需要的资源文件,并让他们放置它在同一个文件夹中。 This is unbelievably inconvenient and i know that there is a way to automatically package these resources into the runnable jar file.这非常不方便,我知道有一种方法可以自动将 package 这些资源放入可运行的 jar 文件中。 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.如果我不能这样做,那么我至少想知道如何访问 Runnable Jar 中的文件,这样我就可以简单地将文件复制粘贴到里面。 I have combed the internet and no suggestions have worked.我已经梳理了互联网,没有任何建议有效。 I can run the program properly in eclipse if i use this code:如果我使用以下代码,我可以在 eclipse 中正确运行程序:

    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. 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.任何想法,因为互联网没有很多。

I had this problem too, but my solution was that the program checks if a folder in %app data%/roaming/ called for myApp exists.我也遇到了这个问题,但我的解决方案是程序检查%app data%/roaming/中为 myApp 调用的文件夹是否存在。 If it doesn't the program does a mkdir and makes that folder.如果没有,程序会创建一个mkdir并创建该文件夹。 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.无需曲棍球来处理您的文件。

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.在 Eclipse 中,右键单击您的项目并导航到新建>文件夹,随意调用此文件夹。 Right-click on your project again and navigate to Properties.再次右键单击您的项目并导航到“属性”。 In this window click on Java Build Path and then on the Libraries tab.在此 window 单击 Java 构建路径,然后单击库选项卡。 Click Add Class Folder and select the folder you created earlier.单击添加 Class 文件夹和 select 您之前创建的文件夹。 This is where you can put your resource files.这是您可以放置资源文件的地方。

Accessing Your Resources访问您的资源

In your Java code you can use the following:在您的 Java 代码中,您可以使用以下内容:

%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.其中%CLASSNAME%是您当前所在的 class 的名称, %FILENAME%是您希望访问的文件的名称。 This will return an InputStream .这将返回一个InputStream If you wish to load an image you can simply wrap it with a call to ImageIO.read(stream) for example:如果你想加载一个图像,你可以简单地通过调用ImageIO.read(stream)来包装它,例如:

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.您不能使用File ,因为它代表文件系统上的实际物理文件,当您将 package 转换为 jar 时,它是 jar 的一部分,而不是文件系统上的实际文件。

You should use:你应该使用:

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

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?将 JAR 文件和资源文件放在同一目录中,而不是 package 将其全部放在 JAR 中,有那么糟糕吗?

You could package your game on an Installer which copies all of it to the Program's folder and puts some shortcuts.您可以 package 您的游戏在安装程序上复制所有到程序的文件夹并放置一些快捷方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM