Runnable 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);