Load jar files [Solved]

Hi guys,

I can’t figure out the easiest way to load files like .txt files.

I have a “levels” package who (will) contain the levels which will be parsed to create the scene, but how do I load the file “levels/1.txt” for example?

I know there is, or there was a ResourceLocatorTool, when i tried to use it jMP said it couldn’t find it, is it still the “standard way”?

I think it isn’t so much code, so can you write the lines to get the URL or the File (which is what i need) to get that file (“levels/1.txt”) too?

If this is important data you should write your own asset manager loader for this so that your app stays platform-independent. Otherwise you can also use the class path to load your data (as long as its really on the class path) like MyGame.class.getResourceAsStream(“levels/1.txt”);

I had to modify (“levels/1.txt”) with ("…/levels/1.txt").getFile().replace("%20", " "), because the path starts at the MyGame package so i have to go back once with “…/”, and, the path has a space that is converted to %20 link a HTML space, works fine in the IDE but has problems when i try to launch the executable jar, or from the Mac’s .app.

Retrying tomorrow, with the ClassLoader class maybe, i heard it works better with the jar.

Btw, maybe in a future you could extend the AssetManager to load any resource as easily as models or create a new class if the AssetManager is intended only for loading scene stuff, it’s just an idea.

If i had had more knowledge about files i would have a try, but i can’t even open this text file without incurring in some exception somewhere XD.

Space characters are not allowed on classpath, just have a class file in the same folder to avoid relative paths.

I found that using getResource instead of the GetResourceAsStream it’s a big mistake. I needed a BufferedReader, so this is the solution that finally works with either the IDE and the jar.

br = new BufferedReader(new InputStreamReader(ClassLoader.getSystemResourceAsStream(“levels/” + file), “UTF-8”));

where file is just the name of the file, in this case “1.txt”.

I like the ClassLoader class because it starts at the root of the packages, not where the MyGame is located, so i don’t have to go back and i think this is a more flexible solution.

It seems the classpath isn’t bothered by the spaces with the AsStream version, i removed the replace(…) infact.

The solution it’s simple i would really liked if i had found it in a tutorial, maybe a note in the AssetManager tutorial showing the difference between loading an AssetManager supported thing and not.

The case is closed, i’ll put [Solved] in the title. Thanks for support normen, and for the bad english too, if i wrote something wrong.