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

**URL:** <https://discourse.mapeditor.org/t/java-is-it-possible-to-load-tmx-tsx-files-from-within-the-jar-itself-libtiled-jar/3644>\
**Category:** Questions\
**Created:** [January 2, 2019, 1:55pm UTC](https://discourse.mapeditor.org/t/java-is-it-possible-to-load-tmx-tsx-files-from-within-the-jar-itself-libtiled-jar/3644 "2019-01-02T13:55:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Skijearz](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@Skijearz](https://discourse.mapeditor.org/u/Skijearz)\
**Post date:** [January 2, 2019, 1:55pm UTC](https://discourse.mapeditor.org/t/java-is-it-possible-to-load-tmx-tsx-files-from-within-the-jar-itself-libtiled-jar/3644/1 "2019-01-02T13:55:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![baylej](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/baylej/32/1381_2.png) [@baylej](https://discourse.mapeditor.org/u/baylej)\
**Post date:** [January 3, 2019, 5:27pm UTC](https://discourse.mapeditor.org/t/java-is-it-possible-to-load-tmx-tsx-files-from-within-the-jar-itself-libtiled-jar/3644/2 "2019-01-03T17:27:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Skijearz](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@Skijearz](https://discourse.mapeditor.org/u/Skijearz)\
**Post date:** [January 7, 2019, 12:48am UTC](https://discourse.mapeditor.org/t/java-is-it-possible-to-load-tmx-tsx-files-from-within-the-jar-itself-libtiled-jar/3644/3 "2019-01-07T00:48:06Z")

</div>

First of all thanks baylej,

i tried your suggestion like this :

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

but then i will get this error :

```auto
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 :

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

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

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

do you have any suggestions to fix this ?

---

<div class="post-metadata">

**Author:** ![baylej](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/baylej/32/1381_2.png) [@baylej](https://discourse.mapeditor.org/u/baylej)\
**Post date:** [January 8, 2019, 5:04pm UTC](https://discourse.mapeditor.org/t/java-is-it-possible-to-load-tmx-tsx-files-from-within-the-jar-itself-libtiled-jar/3644/4 "2019-01-08T17:04:29Z")

</div>

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 🙁

---

<div class="post-metadata">

**Author:** ![Skijearz](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@Skijearz](https://discourse.mapeditor.org/u/Skijearz)\
**Post date:** [January 13, 2019, 9:01am UTC](https://discourse.mapeditor.org/t/java-is-it-possible-to-load-tmx-tsx-files-from-within-the-jar-itself-libtiled-jar/3644/5 "2019-01-13T09:01:03Z")

</div>

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 😃  
But i really appreciate your help.

---

<div class="post-metadata">

**Author:** ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)\
**Post date:** [January 15, 2019, 8:01am UTC](https://discourse.mapeditor.org/t/java-is-it-possible-to-load-tmx-tsx-files-from-within-the-jar-itself-libtiled-jar/3644/6 "2019-01-15T08:01:10Z")

</div>

> [@Skijearz](#):
>
> I just wanted to let you know that i got a working build of the libtiled-java library.

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.

---

<div class="post-metadata">

**Author:** ![Joha\_Tam](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/joha_tam/32/3251_2.png) [@Joha\_Tam](https://discourse.mapeditor.org/u/Joha_Tam)\
**Post date:** [April 13, 2021, 9:48pm UTC](https://discourse.mapeditor.org/t/java-is-it-possible-to-load-tmx-tsx-files-from-within-the-jar-itself-libtiled-jar/3644/7 "2021-04-13T21:48:41Z")

</div>

How did you solve this problem?

---

<div class="post-metadata">

**Author:** ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)\
**Post date:** [April 14, 2021, 8:34am UTC](https://discourse.mapeditor.org/t/java-is-it-possible-to-load-tmx-tsx-files-from-within-the-jar-itself-libtiled-jar/3644/8 "2021-04-14T08:34:38Z")

</div>

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

> <https://github.com/mapeditor/tiled/commit/355935c6bd02f669971bce928c9618b685a91708#diff-26049a29df4aa467611cfd0f97567b9ec076bfbe49e3774805d6f8e92c051eaf>

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.
