[英]java class getResource can't find icons listed in jar file
我真的很難過。 我只是一個舊的 C X11/Motif 程序員,試圖編寫一個小 Java 程序。 在閱讀了一周的 Oracle Java 文檔以及與 getResource 相關的 Stack Overflow 答案后,我仍然無法弄清楚如何在我的 Z68995FCBF432492D15484DDAC04 文件中檢索圖標文件的路徑
我的圖標包含在我的應用程序的 jar 文件中。 我希望使用 jar 文件中的相對 position 訪問它們。 我假設最好的方法是通過 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
這讓我很沮喪。 無論我嘗試什么(我已經嘗試了很多事情),結果都是一樣的。 我不斷收到 NULL 以獲取 getResource 方法的響應。 當我使用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 項目瀏覽器
我將圖像目錄添加到我的項目 Java 構建在 eclipse 中,如下所示: Eclipse ZD52387AZ2D3 構建
我使用 Eclipse 版本:2021-12 (4.22.0) 構建 ID:20211202-1639 構建了程序。 此外,我在 Windows 11 Pro build 22000.434 上使用 Java 17.0.1 2021-10-19 LTS。
您必須在資源前面添加一個斜杠:
Fud.class.getResource("/prev.png");
否則 java 在與 class 相同的文件夾中搜索位於:
所以它會在localhost/system1
中搜索
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.