java - 使用 .jar Applet 加载资源

标签 java jar applet embedded-resource getresource

我制作了一个简单的小程序,它加载包含在我制作的 .jar 文件中的图像。该项目有一个名为“kap12”的包和一个名为“U1”的类。我的图像所在的位置有一个名为“resrc”的文件夹。此文件夹位于根目录中(与 kap12 相同的文件夹)。

这是目录结构:

test.jar
   *kap12
      -U1.class
   *resrc
      -django.jpg

test.html //outside the .jar, same folder as where test.jar is located

当我在 Eclipse 中使用 Appletviewer(没有 .jar)运行该程序时,该程序工作正常。它找到图像并显示它。但是当我用 .jar 在 html 中运行它时,程序找不到它。我每次都会遇到空指针异常。这是我的 .html:

<HTML>
<HEAD>
</HEAD>
<TITLE> U1 </TITLE>
<BODY BGCOLOR="EEEEEE">
<CENTER>
<P ALIGN=CENTER>
<APPLET code="kap12/U1.class"
archive="test.jar"
width="1280"
height="720">
</APPLET>
</P>
</CENTER>
</BODY>
</HTML>

这是我的 SSCCE:

package kap12;

import java.awt.FlowLayout;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JLabel;

public class U1 extends JApplet {

JLabel lbl0;
JLabel img0;
URL imgURL;

ImageIcon django = createImageIcon("resrc/django.jpg");

public void init() {

    setLayout(new FlowLayout());
    lbl0 = new JLabel("Test");
    img0 = new JLabel(django);      
    getContentPane().add(img0);
    getContentPane().add(lbl0);
    img0.setVisible(true);
}

/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = U1.class.getClassLoader().getResource(path);
    if (imgURL != null) {
        System.out.println(""+imgURL);
        return new ImageIcon(imgURL);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}
}

我已经使用 getClass() 和 getClass().getClassLoader() 进行了测试。我已经测试过在字符串路径中写入“/resrc/django.jpg”。我已经测试过使用和不使用 .jar 的情况。我已经测试过将 resrc-folder 添加到 kap12 包文件夹中。看来我已经测试了一切。我花了几天时间试图让它发挥作用。

我应该写什么?我在 StackOverflow 上阅读了很多有关此问题的帖子,但没有成功。谢谢。

更新:我已经测试了你写的所有内容,安德鲁,但无论如何都找不到它。

这是我从 -tvf 得到的:

C:\Program\Java\jdk1.7.0_40\bin>jar -tvf D:\Path\test.jar 2014 年欧洲中部时间 2014 年 1 月 20 日星期一 14:48:14 META-INF/MANIFEST.MF 2035 年 1 月 20 日星期一 14:48:04 CET 2014 pkg/U1.class 496057 2013 年 2 月 3 日星期日 15:25:06 CET resrc/django.jpg

最佳答案

ImageIcon django = createImageIcon("resrc/django.jpg");

应该是:

ImageIcon django = createImageIcon("/resrc/django.jpg");

更新

Don't know exactly how to do just that but I killed "Java SE" from Task Manager, then I deleted the old .jar-file. I have a JOptionPane.showMessageDialog which says what "String path" contains and it seems to update fine.

好的,“更新良好”是重要的部分。对每个测试都做同样的事情。

刚刚注意到加载方法的更详细的细节:

protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = U1.class.getClassLoader().getResource(path);

更安全的是:

protected ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = this.getClass().getResource(path);

我知道我指定的字符串适用于“上下文类加载器”,但无法从 static 方法调用。

<小时/>

如果仍然失败,请转到命令行(例如 Windows 命令提示符或 Linux/Unix 控制台,并提供以下输出:

> jar -tvf path/to/the/test.jar

关于java - 使用 .jar Applet 加载资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21163419/

相关文章:

java - Rad 上传 Java 小程序和 z-index

java - 如何从 Set 和 Comparator 获取列表

java - Hadoop FileAlreadyExistsException : Output directory hdfs://<namenode public dns>:9000/input already exists

jar - 将 Storm 拓扑转换为 flink - "no entry class specified"?

java - 移植库时首先要做的事情

java - java xml 对非 ascii 字符的解析不正确

Java 小程序无法使用 ImageIcon 找到资源图像,除非使用整个路径

java - 从Java 8对象列表中获取具有最大日期属性的对象

java - Spring In CorrectResultSizeDataAccessException 查询返回 Long 值? java

java - 使用java从PostgreSQL中的bytea检索文件