图像不会从jar文件加载 [英] Images won't load from jar file

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

问题描述

我尝试过搜索但仍然没有得到解决方案。当我导出到jar文件时,我的所有图像突然不再起作用了。它们在eclipse中运行良好,我确保它们在jar文件中。我已经尝试使用get资源方法并使图像加载器类自动上升到目录树但它仍然失败。以下是我所做的:

I have tried searching and still haven't got the solution. When I export to a jar file all my images suddenly do not work anymore. They run fine out of eclipse and I have made sure that they are in the jar file. I have tried using get resource methods and making a image-loader class to automatically go up the directory tree but it still fails. Here is what I have done:

public static Image load(String path)
{   String temp = path;
    Image image = null;
    try {

     image = new ImageIcon(path).getImage();


    } 

    catch ( Exception e )
    {   
        try {
         while (image == null )
         {
              image = new ImageIcon("../"+ path).getImage();
         }
         if ( path.equals("../../../../../../"+ temp))
         {
             while ( image ==null)
             {
                 image = new ImageIcon("./"+ path).getImage();
             }
         }
        }
        catch ( Exception ae)
        {


        System.err.println("cannot locate image");
    }} 


    return image;
}   

我将发送的路径如下:doc / icon.png

the path I will send it look as follows: "doc/icon.png"

我已将所有图片放在doc文件夹中,结构是最终的Project,里面有doc文件夹,然后是包含所有包的src文件夹。

I have placed all my images in the doc folder, the structure is final Project, inside there is doc folder and then the src folder that contains all the packages.

推荐答案

尝试使用:

Image.class.getClassLoader().getResource("/filepath/filename").getFile()

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

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