【发布时间】:2022-01-23 17:13:53
【问题描述】:
我真的很难过。我只是一个试图编写一个小的 Java 程序的老 C X11/Motif 程序员。经过一周的阅读the Oracle Java Documentation,以及 Stack Overflow 与 getResource 相关的答案,我仍然不知道如何在我的 jar 文件中检索图标文件的路径。
我的图标包含在我的应用程序的 jar 文件中。我希望使用 jar 文件中的相对位置来访问它们。我假设最好的方法是通过 getResource 方法。
我的程序代码的核心部分称为 Fŭd(发音为 food - 就像漫画中的猫拼写的那样“Get Fuzzy") 如下:
package localhost.system1;
imports not shown for brevity.
public class Fud extends JPanel
implements FocusListener, ActionListener, ItemListener
{
private static final long serialVersionUID = 1L;
static Food data = null;
static int prev = 0;
static int next = 1;
static int plus = 2;
static int minus = 3;
public static void main(String args[]) throws Exception
{
LocalDate now = LocalDate.now();
int dateDifference = 0;
// load in the existing data
data = new Food(programName);
data.loadFood(programName);
// test to see if data is up to date. Add days if not
dateDifference = Math.abs((int)ChronoUnit.DAYS.between(now, data.day[0].date));
if ( dateDifference != 0)
{
data.adjustToToday(dateDifference, programName);
}
/////////////////////////////////////////////////
// create the GUI and switch running over to it.
/////////////////////////////////////////////////
Fud fud = new Fud();
Class<? extends Fud> fudClass = fud.getClass();
String className = fudClass.getName();
System.out.println("fudClass getname returns " + className);
URL testURL = fudClass.getResource("prev.png");
System.out.println("fudClass getResource returned " + testURL);
// Create GUI and turn the control over to it
javax.swing.SwingUtilities.invokeLater
(new Runnable()
{
public void run()
{
URL[] iconURL = new URL[4];
iconURL[prev] = Fud.class.getResource("prev.png");
iconURL[next] = Fud.class.getResource("next.png");
iconURL[plus] = Fud.class.getResource("plus.png");
iconURL[minus] = Fud.class.getResource("minus.png");
createAndShowGUI(fud, iconURL);
}
}
);
} // end of main
.
.
.
Rest of methods and subroutines needed
.
.
.
}
运行时,代码返回如下结果:
fudClass getname returns localhost.system1.Fud
fudClass getResource returned null
这让我很沮丧。无论我尝试什么(我已经尝试了很多事情),结果都是一样的。对于 getResource 方法的响应,我不断收到 NULL。当我使用 jar -tf Fud.jar 查询 jar 文件时,我得到以下信息:
jar tf Fud.jar
META-INF/MANIFEST.MF
localhost/
localhost/system1/
localhost/system1/Day.class
localhost/system1/Food.class
localhost/system1/Fud$1.class
localhost/system1/Fud$2.class
localhost/system1/Fud$3.class
localhost/system1/Fud$4.class
localhost/system1/Fud$5.class
localhost/system1/Fud$6.class
localhost/system1/Fud$7.class
localhost/system1/Fud.class
minus.png
next.png
plus.png
prev.png
所以图标在 Jar 文件中。 谁能告诉我我做错了什么?在 Eclipse 中,我的项目浏览器看起来像:eclipse Project Explorer
我在 Eclipse 中将 Image 目录添加到我的项目 Java 构建中,如下所示:Eclipse Java Build
我使用 Eclipse 版本构建程序:2021-12 (4.22.0) 构建 ID:20211202-1639。此外,我在 Windows 11 Pro build 22000.434 上使用 Java 17.0.1 2021-10-19 LTS。
【问题讨论】:
-
更仔细地阅读该文档,尤其是两个要点。
fudClass.getResource("prev.png")查找名为localhost/system1/prev.png的 jar 条目。fudClass.getResource("/prev.png")(注意字符串中的前导斜杠)查找名为prev.png的 jar 条目。 (这是设计使然;资源应该在包内,以减少与其他库的 jar 文件冲突的风险。)
标签: java eclipse getresource