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


#1

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?


(Jonathan Bayle) #2

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.


#3

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 ?


(Jonathan Bayle) #4

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:


#5

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.


(Thorbjørn Lindeijer) #6

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.