简体   繁体   中英

src folder not included in exported .jar messing up resource paths (Eclipse)

So I have a project that runs perfectly within Eclipse, but when I export it as a runnable .jar the paths to my .png resource files won't work anymore as the src folder is not included in the exported .jar.

createTexture("resources/grass.png", 0);

This is where I use the path.

public static void createTexture(String path, int index){
    try (InputStream inputStream = Drawer.class.getClassLoader().getResourceAsStream(path)){
        textureDecoder = new PNGDecoder(inputStream); [...]

now I get the error

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)

OLD: This is obviously the error I get when I try to run the .jar:

java.io.FileNotFoundException: src\resources\grass.png

I'll of course include more code/configurations I've made if that would be relevant.

EDIT: Problem solved, thanks!

采用:

InputStream inputStream = className.class.getClassLoader().getResourceAsStream(path);

In Eclipse, after selecting "Export ..." and "JAR File", there is checkbox for "Export Java source files and resources". You need to click that checkbox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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