繁体   English   中英

可运行的 Jar 文件不加载资源

[英]Runnable Jar File doesn't load resources

在 IntelliJ 中,我可以运行:

ImageIcon icon = new ImageIcon("src/de/therealjan/tools/pictures/pic.png");

但是在一个无法运行的可运行 JAR 文件中。 我已经尝试过“/pictures/pic.png”,但这也不起作用。

有任何想法吗?

您应该在类路径或类路径中的目录中添加图像,然后像这样加载图像:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL resource = classLoader.getResource("src/de/therealjan/tools/pictures/pic.png");
ImageIcon icon = new ImageIcon(resource);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM