简体   繁体   English

ClassLoader.getResource()在.jar文件中不起作用

[英]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.

相关问题 classLoader.getResource在jar文件中不起作用 - classLoader.getResource doesn't work in jar file ClassLoader.getResource()对jar中的图标文件不起作用。 图标必须存在于包含jar的文件夹中 - ClassLoader.getResource() doesn't work for the icon files in a jar. The icons need to be present in the folder containing jar classloader.getResource返回带有war文件的路径 - classloader.getResource return a path with war file Class.getResource()和ClassLoader.getResource()在可执行jar中的奇怪行为 - Strange behavior of Class.getResource() and ClassLoader.getResource() in executable jar NullPointerException on ClassLoader.getResource() - NullPointerException on ClassLoader.getResource() 无法使用classLoader.getResource(fileName).getFile()获取文件 - unable to get file using classLoader.getResource(fileName).getFile() classLoader.getResource(“。”)是/ bin文件夹 - classLoader.getResource(“.”) is /bin folder 使用ClassLoader.getResource()加载BufferedImage - loading BufferedImage with ClassLoader.getResource() ClassLoader.getResource在使用File.getAbsolutePath时返回null - ClassLoader.getResource returning null while using File.getAbsolutePath classLoader.getResource(filename) 不返回现有文件(返回 null) - classLoader.getResource(filename) not returning existent file (returning null)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM