如何在 .jar 中使用纹理?
How do I use textures in a .jar?
我正在使用 LWJGL 和 Slick-Util。我的纹理加载在 Eclipse 中工作正常,但是当我将它导出到 .jar 时,它给出 java.lang.RuntimeException: Resource not found: res/test/texture.png
这是我的纹理加载代码:
public int loadTexture(String file) {
Texture tex = null;
try {
tex = TextureLoader.getTexture("PNG", new FileInputStream("res/" + file + ".png"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
int texID = tex.getTextureID();
textures.add(texID);
return texID;
}
您可能没有 运行 正确目录中的应用程序。为了使您的代码正常工作,您需要在包含 res
文件夹的目录中启动程序。
或者您可以使用
...
try {
tex = TextureLoader.getTexture("PNG", this.getClass().getResourceAsStream("/res/" + file + ".png");
} catch (FileNotFoundException e) {
...
}
但在这种情况下 res
文件夹需要添加到类路径中:
java -cp .;path/to/res;some.jar your.main.Class
使用 OctoArcher
摆脱 getResource()/getResourceAsStream()
我正在使用 LWJGL 和 Slick-Util。我的纹理加载在 Eclipse 中工作正常,但是当我将它导出到 .jar 时,它给出 java.lang.RuntimeException: Resource not found: res/test/texture.png
这是我的纹理加载代码:
public int loadTexture(String file) {
Texture tex = null;
try {
tex = TextureLoader.getTexture("PNG", new FileInputStream("res/" + file + ".png"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
int texID = tex.getTextureID();
textures.add(texID);
return texID;
}
您可能没有 运行 正确目录中的应用程序。为了使您的代码正常工作,您需要在包含 res
文件夹的目录中启动程序。
或者您可以使用
...
try {
tex = TextureLoader.getTexture("PNG", this.getClass().getResourceAsStream("/res/" + file + ".png");
} catch (FileNotFoundException e) {
...
}
但在这种情况下 res
文件夹需要添加到类路径中:
java -cp .;path/to/res;some.jar your.main.Class
使用 OctoArcher
摆脱 getResource()/getResourceAsStream()