[Java] Is it possible to load tmx/tsx files from within the jar itself?(libtiled.jar)

Hello Tiled Community,

Im using the libtiled-java to load my .tmx maps and everything works fine if they are located as an external resource and not in the .jar itself, which modification would be needed to load the maps from the jar itself?

Hi,

As it is possible to load a classpath resource as an InputStream (ClassLoader.getResourceAsStream(String)), you should be able to use the TMXMapReader.readMap(InputStream) and TMXMapReader.readTileset(InputStream) methods.

First of all thanks baylej,

i tried your suggestion like this :

tileSet = mapReader.readTileset(ClassLoader.getSystemResourceAsStream("rsc/Room_blue.tsx"));
map = mapReader.readMap(ClassLoader.getSystemResourceAsStream("rsc/map1.tmx")) ;

but then i will get this error :

javax.imageio.IIOException: Can't read input file!

this is caused, because the mapReader can’t get the image from the tileset because the path is not the same as the inputStream of the tileset/map itself.

also if i only use :

map = mapReader.readMap(ClassLoader.getSystemResourceAsStream("rsc/map1.tmx")) ;

i will get an error that the tileset is not found :

java.io.FileNotFoundException: C:\..\..\..\semester_3_projekt\DarkIT\Room_blue.tsx

do you have any suggestions to fix this ?

Ok, I see what the problem is, the library parses the tileset (in your second try, the map) but fails to open referenced resources (in your first try, an image, hence the imageio exception, and in your second try the tileset).
You could embed the tileset in the map, but then there is still the image that won’t load, you’ll have to hack the libtiled-java to implement your use case then :slightly_frowning_face:

Hey @baylej ,
I just wanted to let you know that i got a working build of the libtiled-java library.
Now i can load my tmx/tsx files from insed my build jar :smiley:
But i really appreciate your help.

Do you think it would be possible to support both use-cases from within the library? It would be great if you could do a pull request, if you’re interested in making this work.

How did you solve this problem?

@Joha_Tam I think this problem was resolved with the following change:

Please check whether your version of libtiled-java has this change. The above change even added a test case where the map gets loaded from the JAR.