[英]ClassLoader.getResource() doesn't work from .jar file
I have the following code: 我有以下代码:
private final ImageIcon placeHolder = new ImageIcon(this.getClass().getClassLoader().getResource("Cards\\trans.png"));
But this does not work once my application exported into a .jar
file. 但是,一旦我的应用程序导出到.jar
文件中,这就不起作用了。
Your "\\t" is being compiled as a tab - you need to escape it: 您的“\\ t”正在编译为选项卡 - 您需要将其转义:
private final ImageIcon placeHolder = new ImageIcon(
this.getClass().getClassLoader().getResource("Cards\\trans.png"));
Note the double backslash. 注意双反斜杠。 This may not be the only thing wrong, of course - but it's a start... 当然,这可能不是唯一的错误 - 但这是一个开始......
In fact, I would specify it with a forward slash instead. 实际上,我会用正斜杠来指定它。 That works on both Windows and Unix-based OSes anyway, and it also works with jar files. 无论如何,这适用于Windows和基于Unix的操作系统,它也适用于jar文件。 The only reason I highlighted the double backslash was to raise the point of string escaping in general. 我突出显示双反斜杠的唯一原因是提高字符串转义点。 Try this: 尝试这个:
private final ImageIcon placeHolder = new ImageIcon(
this.getClass().getClassLoader().getResource("Cards/trans.png"));
Next, make sure you've got the exact correct name for the file, including case. 接下来,确保您已获得文件的确切名称,包括大小写。 Even though Windows file systems aren't generally case-sensitive, jar files are. 即使Windows文件系统通常不区分大小写,jar文件也是如此。 If it's actually "cards" instead of "Cards" or "TRANS.png" instead of "trans.png", it won't work. 如果它实际上是“卡片”而不是“卡片”或“TRANS.png”而不是“trans.png”,它将无效。
用它来获取jar文件中的图像
ClassLoader.getSystemResource("images/Springz3.png"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.