无法从Jar加载图像 [英] Unable to load image from Jar

查看:117
本文介绍了无法从Jar加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Jar文件中加载图像。这是行:

I'm trying to load an image from a Jar File. Here's the line:

Image imgTrayIcon = new Image(display, this.getClass().getResourceAsStream("icon.ico"));

我见过很多使用这种方法的例子但是当我尝试这样做时,我得到了错误说我的形象无效。这是堆栈跟踪:

I've seen many examples using this method but when I try to do so, I get and error saying that my image is invalid. Here's the stack trace:

 [java] Exception in thread "Thread-0" org.eclipse.swt.SWTException: Invalid image
 [java]     at org.eclipse.swt.SWT.error(SWT.java:4083)
 [java]     at org.eclipse.swt.SWT.error(SWT.java:3998)
 [java]     at org.eclipse.swt.SWT.error(SWT.java:3969)
 [java]     at org.eclipse.swt.internal.image.WinICOFileFormat.loadInfoHeader(WinICOFileFormat.java:200)
 [java]     at org.eclipse.swt.internal.image.WinICOFileFormat.loadIcon(WinICOFileFormat.java:127)
 [java]     at org.eclipse.swt.internal.image.WinICOFileFormat.loadFromByteStream(WinICOFileFormat.java:119)
 [java]     at org.eclipse.swt.internal.image.FileFormat.loadFromStream(FileFormat.java:48)
 [java]     at org.eclipse.swt.internal.image.FileFormat.load(FileFormat.java:84)
 [java]     at org.eclipse.swt.graphics.ImageLoader.load(ImageLoader.java:130)
 [java]     at org.eclipse.swt.graphics.ImageDataLoader.load(ImageDataLoader.java:22)
 [java]     at org.eclipse.swt.graphics.ImageData.<init>(ImageData.java:331)
 [java]     at org.eclipse.swt.graphics.Image.<init>(Image.java:545)
 [java]     at SysTray.run(Unknown Source)

我正在使用的图标绝对有效。我用图标工具检查了这个。我也尝试将图标放在与我的代码相同的目录中(而不是Jar-ing)并像这样使用它:

The icon I'm using is definitely valid. I've checked this using icon tools. I've also tried placing the icon in the same directory as my code (not Jar-ing it) and using it like this:

Image imgTrayIcon = new Image(display, "icon.ico");

这很好用,但是当我尝试把它放在Jar中时,它没有。我似乎无法弄清楚为什么会这样。我已经解压缩了我的Jar,检查文件是否已添加到Jar中,它似乎就在那里。我的jar没有任何复杂的文件夹结构。所有文件和资源都在同一级别的树中。

This works just fine, but when I try to put it in the Jar, it doesn't. I can't seem to figure out why this is happening. I've uncompressed my Jar to check whether the file was added to the Jar and it seems to be there. My jar doesn't has any complex folder structure. All the files and resources are in the same level of tree.

这里有什么问题吗?谢谢

Any ideas on what's wrong here? Thanks

以下是一些复制问题的示例代码:

Here's some sample code to replicate the issue:

Example.java

Example.java

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.*;

class Example {

    public static void main(String[] args) {
        Display display = Display.getDefault();
        Image imgTrayIcon = new Image(display, Example.class.getClassLoader().getResourceAsStream("icon.ico"));
    }

}

命令:

javac -cp "SWT.jar" Example.java
jar cf Example.jar *.class *.ico
java -cp "Example.jar;SWT.jar" Example


推荐答案

好像你正在研究多线程应用程序。
来自堆栈跟踪线程Thread-0中的异常org.eclipse.swt.SWTException:图像无效您似乎正在加载图像非ui线程并尝试在某些UI元素中使用它。 见到这一点

Seems like you are working on multi-threaded application. From the stack-trace Exception in thread "Thread-0" org.eclipse.swt.SWTException: Invalid image it appears that you are loading the image in a non-ui thread and trying to use that in some UI element. See this.

在UI线程中,下面的代码工作正常。 (图标文件位于测试包内)

Within a UI thread the below code works fine. (The icon file is inside the test package)

package test;

import java.io.InputStream;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class IconTest 
{
    public static void main(String[] args)
    {
        final Display display = new Display();

        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        shell.setSize(200, 200);
        shell.setLocation(20, 20);

        InputStream stream = IconTest.class.getResourceAsStream("/test/icon.ico"); 

        Image imgTrayIcon = new Image(display, stream);

        shell.setImage(imgTrayIcon);

        shell.open();
        while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                        display.sleep();
        }

        if(imgTrayIcon != null)
            imgTrayIcon.dispose();

        display.dispose();
    }
}

这篇关于无法从Jar加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆