图像不会从jar文件加载

LL.*_*LL. -1 java jar image

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

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;
}   
Run Code Online (Sandbox Code Playgroud)

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

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

Bal*_*ako 5

试试这个:

Image.class.getClassLoader().getResource("/filepath/filename").getFile()
Run Code Online (Sandbox Code Playgroud)