简体   繁体   English

java class getResource 找不到 jar 文件中列出的图标

[英]java class getResource can't find icons listed in jar file

I am really stumped.我真的很难过。 I'm just an old C X11/Motif programmer trying to write a little Java program.我只是一个旧的 C X11/Motif 程序员,试图编写一个小 Java 程序。 After a week of reading the Oracle Java Documentation , as well as the Stack Overflow answers related to getResource, I still can not figure out how to retrieve the path to the icon files in my jar file.在阅读了一周的 Oracle Java 文档以及与 getResource 相关的 Stack Overflow 答案后,我仍然无法弄清楚如何在我的 Z68995FCBF432492D15484DDAC04 文件中检索图标文件的路径

My icons are contained within the jar file for my application.我的图标包含在我的应用程序的 jar 文件中。 I wish to access them using the relative position within jar file.我希望使用 jar 文件中的相对 position 访问它们。 I am assuming the best way to do this is through the getResource method.我假设最好的方法是通过 getResource 方法。

The core part of my code for my program called Fŭd (pronounced food - like the cat spells it in the comic strip " Get Fuzzy ") is as follows:我的程序代码的核心部分称为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
.
.
.
}

When run, the code returns the following results:运行时,代码返回以下结果:

fudClass getname returns localhost.system1.Fud
fudClass getResource returned null

This has me quite frustrated.这让我很沮丧。 No matter what I try (and I have tried a number of things) the result remains the same.无论我尝试什么(我已经尝试了很多事情),结果都是一样的。 I keep getting NULL for a response from the getResource method.我不断收到 NULL 以获取 getResource 方法的响应。 When I query the jar file with jar -tf Fud.jar I get the following:当我使用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

So the icons are in the Jar file.所以图标在 Jar 文件中。 Can anyone tell me what I am doing wrong?谁能告诉我我做错了什么? In Eclipse, my project explorer looks like: eclipse Project Explorer在 Eclipse 中,我的项目浏览器看起来像: eclipse 项目浏览器

I added the Image directory to my project Java build in eclipse as follows: Eclipse Java Build我将图像目录添加到我的项目 Java 构建在 eclipse 中,如下所示: Eclipse ZD52387AZ2D3 构建

I built the program using Eclipse Version: 2021-12 (4.22.0) Build id: 20211202-1639.我使用 Eclipse 版本:2021-12 (4.22.0) 构建 ID:20211202-1639 构建了程序。 Furthermore, I am using Java 17.0.1 2021-10-19 LTS on Windows 11 Pro build 22000.434.此外,我在 Windows 11 Pro build 22000.434 上使用 Java 17.0.1 2021-10-19 LTS。

You have to add a slash in front of the resource:您必须在资源前面添加一个斜杠:

Fud.class.getResource("/prev.png");

otherwise java searching in the same folder as the class is located:否则 java 在与 class 相同的文件夹中搜索位于:

So it will search in localhost/system1所以它会在localhost/system1中搜索

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

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