Loading files after export

Hi everybody

I am trying to make a game, and have run into a problem.

I have made a game, which consists of several levels.
Each level is saved as an xml-file, which is loaded and parsed.


loadLevel(new File("resources/levels/"+levelID));

Everything has been working until I tried to export the program, after exporting the program I can’t find a way to load my level-files.
I have tried to place my files within the game-jar, and I have tried to place them at the same level as my jar-file, but I can’t seem to reach them.

So what am I doing wrong?

Best wishes
Blaa Vogn

That’s because you’re using File, which doesn’t work inside a jar. You need to access it as a resource instead.

If you’re using libGDX, use Gdx.files.internal instead. Slick also has its own resource loader, though I’m not sure you’re any better off using it.

Thank you very much it is working, and I only need music for my game to be complete :D.

My only problem is now, that when I try to compile and run the program with Eclipse, I can’t find the files.

I guess my project has the wrong configuration, I have tried to include the resource folder to the “Java build path”, but that doesn’t help. Any ideas?

I could just either export the game each time I want to try it, or run with two different version, but it seems rather frustrating.

Project/
…/bin/
…/res/
…/res/myres_name_blabla.txt
…/src/

Include “res” into java build path and use

URL url = this.getClass().getClassLoader().getResource( "myres_name_blabla.txt" );

Okay, it is working.

I don’t quite understand why “myres_name_blabla.txt”, is correct and “res/myres_name_blabla.txt” isn’t, but it is working!

because if you add res to your build path, it takes the contents of res and puts them in the jar…

Indeed, since both src (by default) and now res are in the build path, their CONTENTS will be put into the jar.

If you would have created a folder under “src” (src/res/my_res.txt, for example) then “res/my_res.txt” would work because res is now part of src’s CONTENTS (and res would no longer be in the build path directly).

i use this =)
for in jar - wher: path = res/myres_IN.txt (…/src/res/myres_IN.txt)


		ClassLoader c = ClassLoader.getSystemClassLoader();
		InputStream is = c.getSystemResourceAsStream(path);
		if(is == null){throw new FileNotFoundException(path);}

and for out - wher: path = res/myres_Out.txt (…/res/myres_Out.txt)
// need move res folder near jar when export it
out have bug - need run like “java -jar blabla.jar”
if you run from out folder like “java -jar folder_A/blabla.jar” (C:/folder_0/folder_A/blabla.jar)
java search “res” in “folder_0” not wher jar itself (not in “folder_A”)^^


		File file = (new File(path));	
		if(!file.exists()){throw new FileNotFoundException(path);}
		InputStream is = new FileInputStream(file);

i prefer out.