导出的 .jar 中未包含 src 文件夹,弄乱了资源路径 (Eclipse)
src folder not included in exported .jar messing up resource paths (Eclipse)
所以我有一个 运行 在 Eclipse 中完美运行的项目,但是当我将它导出为 运行nable .jar 时,我的 .png 资源文件的路径将不再有效,因为导出的 .jar 中不包含 src 文件夹。
createTexture("resources/grass.png", 0);
这是我使用路径的地方。
public static void createTexture(String path, int index){
try (InputStream inputStream = Drawer.class.getClassLoader().getResourceAsStream(path)){
textureDecoder = new PNGDecoder(inputStream); [...]
现在我收到错误
Exception in thread "main" java.lang.NullPointerException
at de.matthiasmann.twl.utils.PNGDecoder.readFully(PNGDecoder.java:802)
at de.matthiasmann.twl.utils.PNGDecoder.<init>(PNGDecoder.java:112)
at workers.Drawer.createTexture(Drawer.java:80)
at workers.Drawer.openglSetup(Drawer.java:44)
at jonta.Main.setUp(Main.java:41)
at jonta.Main.main(Main.java:121)
旧:
这显然是我尝试 运行 .jar:
时遇到的错误
java.io.FileNotFoundException: src\resources\grass.png
我当然会包括更多 code/configurations 如果相关的话,我已经做了。
编辑:问题已解决,谢谢!
使用:
InputStream inputStream = className.class.getClassLoader().getResourceAsStream(path);
在 Eclipse 中,选择 "Export ..." 和 "JAR File" 后,会出现 "Export Java source files and resources" 的复选框。您需要单击该复选框。
所以我有一个 运行 在 Eclipse 中完美运行的项目,但是当我将它导出为 运行nable .jar 时,我的 .png 资源文件的路径将不再有效,因为导出的 .jar 中不包含 src 文件夹。
createTexture("resources/grass.png", 0);
这是我使用路径的地方。
public static void createTexture(String path, int index){
try (InputStream inputStream = Drawer.class.getClassLoader().getResourceAsStream(path)){
textureDecoder = new PNGDecoder(inputStream); [...]
现在我收到错误
Exception in thread "main" java.lang.NullPointerException
at de.matthiasmann.twl.utils.PNGDecoder.readFully(PNGDecoder.java:802)
at de.matthiasmann.twl.utils.PNGDecoder.<init>(PNGDecoder.java:112)
at workers.Drawer.createTexture(Drawer.java:80)
at workers.Drawer.openglSetup(Drawer.java:44)
at jonta.Main.setUp(Main.java:41)
at jonta.Main.main(Main.java:121)
旧: 这显然是我尝试 运行 .jar:
时遇到的错误java.io.FileNotFoundException: src\resources\grass.png
我当然会包括更多 code/configurations 如果相关的话,我已经做了。
编辑:问题已解决,谢谢!
使用:
InputStream inputStream = className.class.getClassLoader().getResourceAsStream(path);
在 Eclipse 中,选择 "Export ..." 和 "JAR File" 后,会出现 "Export Java source files and resources" 的复选框。您需要单击该复选框。