当我在Intellij中运行它时工作正常但是当我尝试将它构建到jar中时,资源文件夹不会添加到jar中。
我写了一些代码来测试我是否弄错了路径......
String file = File.separator + "resources" + File.separator + "components" + File.separator + "game_window.png";
while (!file.isEmpty()) {
System.out.println(file + ": " + ScreenShot.class.getClassLoader().getResourceAsStream(file));
file = file.substring(1);
}
gameWindow = ImageIO.read(ScreenShot.class.getClassLoader().getResourceAsStream("components" + File.separator + "game_window.png"));
在Intellij中产生以下输出:
\ resources \ components \ game_window.png:null
resources \ components \ game_window.png:null
esources \ components \ game_window.png:null
sources \ components \ game_window.png:null
ources \ components \ game_window.png:null
urces \ components \ game_window.png:null
rces \ components \ game_window.png:null
ces \ components \ game_window.png:null
es \ components \ game_window.png:null
s \ components \ game_window.png:null
\ components \ game_window.png:java.io.BufferedInputStream@723279cf
components \ game_window.png:java.io.BufferedInputStream@10f87f48
等...
如果我在jar中运行相同的代码,它会生成以下内容:
\ resources \ components \ game_window.png:null
resources \ components \ game_window.png:null
esources \ components \ game_window.png:null
sources \ components \ game_window.png:null
ources \ components \ game_window.png:null
urces \ components \ game_window.png:null
rces \ components \ game_window.png:null
ces \ components \ game_window.png:null
es \ components \ game_window.png:null
s \ components \ game_window.png:null
\ components \ game_window.png:null
components \ game_window.png:null
等...
我很确定问题是资源文件夹没有被添加到jar中。
同样在这里我是如何建立我的罐子的