jar文件中的图像资源出错

时间:2023-01-28 09:15:30

I'm having a problem with running a jar file. If I compile the code in eclipse everything is ok, but when I export to jar and try to run the program it throws an error. The error is:

我遇到运行jar文件的问题。如果我在eclipse中编译代码一切正常,但是当我导出到jar并尝试运行程序时会抛出错误。错误是:

Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at game.ChessSquare.<init>(ChessSquare.java:17)
    at game.ChessFrame.DrawField(ChessFrame.java:131)
    at game.ChessFrame.<init>(ChessFrame.java:38)
    at game.Chess.main(Chess.java:7)

So the piece of code that throws this error is

因此抛出此错误的代码片段是

//text is something like "pawn" or "king".The whole image is an icon on the button 
img = new ImageIcon(getClass().getResource("white/"+text+".png"));

As I understand, it can't find the image so img variable is null. But I can't understand why then it does run in eclipse. Any help with this? Thanks

据我所知,它无法找到图像所以img变量为null。但是我无法理解为什么然后它会在eclipse中运行。对此有何帮助?谢谢

P.S @AndrewThompson

E:\Desktop>jar tvf chess.jar
49 Sat Feb 11 23:40:06 GMT 2012 META-INF/MANIFEST.MF
378 Sat Feb 11 22:51:04 GMT 2012 game/ChessFieldCoord.class
1932 Sat Feb 11 23:39:48 GMT 2012 game/ChessSquare.class
536 Sat Feb 11 22:51:04 GMT 2012 game/Chess.class
2122 Sat Feb 11 22:51:04 GMT 2012 game/Rook.class
2357 Sat Feb 11 22:51:04 GMT 2012 game/Bishop.class
1946 Sat Feb 11 22:51:04 GMT 2012 game/Queen.class
2127 Sat Feb 11 22:51:04 GMT 2012 game/Pawn.class
1630 Sat Feb 11 22:51:04 GMT 2012 game/Knight.class
878 Sat Feb 11 22:51:04 GMT 2012 game/Empty.class
1642 Sat Feb 11 22:51:04 GMT 2012 game/King.class
915 Sat Feb 11 22:52:32 GMT 2012 game/ChessFrame$1.class
841 Sat Feb 11 22:52:32 GMT 2012 game/ChessFrame$2.class
695 Sat Feb 11 22:52:32 GMT 2012 game/ChessFrame$3.class
3525 Sat Feb 11 22:52:32 GMT 2012 game/ChessFrame$Move.class
4675 Sat Feb 11 22:52:32 GMT 2012 game/ChessFrame.class
997 Sat Feb 11 22:51:04 GMT 2012 game/ChessFigure.class
928 Sat Feb 11 15:49:32 GMT 2012 game/black/bishop.png
1947 Sat Feb 11 15:49:10 GMT 2012 game/black/king.png
1247 Sat Feb 11 15:49:38 GMT 2012 game/black/knight.png
627 Sat Feb 11 15:49:44 GMT 2012 game/black/pawn.png
1668 Sat Feb 11 15:49:20 GMT 2012 game/black/queen.png
626 Sat Feb 11 15:49:26 GMT 2012 game/black/rook.png
1493 Sat Feb 11 15:48:30 GMT 2012 game/white/bishop.png
1889 Sat Feb 11 15:48:06 GMT 2012 game/white/king.png
1544 Sat Feb 11 15:48:40 GMT 2012 game/white/knight.png
1003 Sat Feb 11 15:48:48 GMT 2012 game/white/pawn.png
2440 Sat Feb 11 15:48:12 GMT 2012 game/white/queen.png
837 Sat Feb 11 15:48:20 GMT 2012 game/white/rook.png

The file where the error occurs is ChessSquare.class

发生错误的文件是ChessSquare.class

3 个解决方案

#1


0  

img = new ImageIcon(getClass().getResource("/game/white/"+text+".png"));

Note the leading slash, as well as the game prefix.

注意前导斜杠以及游戏前缀。

#2


0  

As indicated in the comments, look in your jar (use winzip, 7zip, or something like that) to see where the resource is located. If it is not in a "white" directory then something is wrong with your jar build.

如评论中所示,查看您的jar(使用winzip,7zip或类似的东西)以查看资源的位置。如果它不在“白色”目录中,那么你的jar版本就会出错。

As to why it's running in Eclipse... that's likely because Eclipse is including the resource directory in the classpath when you run the program. This means that the getClass().getResource(...) call finds the png.

至于为什么它在Eclipse中运行...这可能是因为Eclipse在运行程序时在类路径中包含资源目录。这意味着getClass()。getResource(...)调用找到png。

If you are keeping the file in a resources directory in Eclipse it also could be exporting the files into the META-INF directory in the jar, look in resources. If it is doing that then you may have setup the jar to be built for deployment into a web environment (see servlet 3.0 fragments).

如果要将文件保存在Eclipse的资源目录中,它也可以将文件导出到jar中的META-INF目录中,查看资源。如果它正在执行该操作,那么您可能已经设置了要部署到Web环境中的jar(请参阅servlet 3.0片段)。

One final note, be careful about using getClass().getResource(...) as it locks you down to only finding resources associated with the ClassLoader of the Class making that call (subject to how the ClassLoader hierarchy in question operates). You may want to provide extensions later where you can package other image sets in jars. In those cases you'll have to rely on slightly different mechanisms to get the images loaded (see ClassLoader.getResources as a starting point).

最后要注意的是,请注意使用getClass()。getResource(...),因为它会将您锁定为仅查找与进行该调用的Class的ClassLoader相关联的资源(取决于所讨论的ClassLoader层次结构的运行方式)。您可能希望稍后提供扩展,您可以在其中将其他图像集打包到jar中。在这些情况下,您将不得不依赖稍微不同的机制来加载图像(请参阅ClassLoader.getResources作为起点)。

EDIT: This looks to be strictly an Eclipse problem. According to the way that the jar is structured the correct path is "game/white/king.png".

编辑:这看起来严格来说是一个Eclipse问题。根据jar的结构方式,正确的路径是“game / white / king.png”。

#3


0  

i'm an idiot, i finally figured out where the error was. As I wrote the output was "file:/E:/Desktop/Chess/bin/game/white/Pawn.png", notice the big P letter in Pawn.png. But the actual images' names started with small letters. That's why it didn't work. Still a mystery for me why then it worked in eclipse

我是个白痴,我终于找到了错误的位置。我写的输出是“file:/ E:/Desktop/Chess/bin/game/white/Pawn.png”,请注意Pawn.png中的大P字母。但实际图像的名称以小写字母开头。这就是为什么它不起作用。对我来说仍然是一个谜,为什么它在日食中起作用

#1


0  

img = new ImageIcon(getClass().getResource("/game/white/"+text+".png"));

Note the leading slash, as well as the game prefix.

注意前导斜杠以及游戏前缀。

#2


0  

As indicated in the comments, look in your jar (use winzip, 7zip, or something like that) to see where the resource is located. If it is not in a "white" directory then something is wrong with your jar build.

如评论中所示,查看您的jar(使用winzip,7zip或类似的东西)以查看资源的位置。如果它不在“白色”目录中,那么你的jar版本就会出错。

As to why it's running in Eclipse... that's likely because Eclipse is including the resource directory in the classpath when you run the program. This means that the getClass().getResource(...) call finds the png.

至于为什么它在Eclipse中运行...这可能是因为Eclipse在运行程序时在类路径中包含资源目录。这意味着getClass()。getResource(...)调用找到png。

If you are keeping the file in a resources directory in Eclipse it also could be exporting the files into the META-INF directory in the jar, look in resources. If it is doing that then you may have setup the jar to be built for deployment into a web environment (see servlet 3.0 fragments).

如果要将文件保存在Eclipse的资源目录中,它也可以将文件导出到jar中的META-INF目录中,查看资源。如果它正在执行该操作,那么您可能已经设置了要部署到Web环境中的jar(请参阅servlet 3.0片段)。

One final note, be careful about using getClass().getResource(...) as it locks you down to only finding resources associated with the ClassLoader of the Class making that call (subject to how the ClassLoader hierarchy in question operates). You may want to provide extensions later where you can package other image sets in jars. In those cases you'll have to rely on slightly different mechanisms to get the images loaded (see ClassLoader.getResources as a starting point).

最后要注意的是,请注意使用getClass()。getResource(...),因为它会将您锁定为仅查找与进行该调用的Class的ClassLoader相关联的资源(取决于所讨论的ClassLoader层次结构的运行方式)。您可能希望稍后提供扩展,您可以在其中将其他图像集打包到jar中。在这些情况下,您将不得不依赖稍微不同的机制来加载图像(请参阅ClassLoader.getResources作为起点)。

EDIT: This looks to be strictly an Eclipse problem. According to the way that the jar is structured the correct path is "game/white/king.png".

编辑:这看起来严格来说是一个Eclipse问题。根据jar的结构方式,正确的路径是“game / white / king.png”。

#3


0  

i'm an idiot, i finally figured out where the error was. As I wrote the output was "file:/E:/Desktop/Chess/bin/game/white/Pawn.png", notice the big P letter in Pawn.png. But the actual images' names started with small letters. That's why it didn't work. Still a mystery for me why then it worked in eclipse

我是个白痴,我终于找到了错误的位置。我写的输出是“file:/ E:/Desktop/Chess/bin/game/white/Pawn.png”,请注意Pawn.png中的大P字母。但实际图像的名称以小写字母开头。这就是为什么它不起作用。对我来说仍然是一个谜,为什么它在日食中起作用