# How do I load tmx files with Slick2d?

I just started using Slick2D and learned how simple it is to load in a tilemap and display it. I tried atleast a dozen different tmx files from numerous examples to see if it was the actual file that was corrupted. Everytime I get this error:

Exception in thread "main" java.lang.RuntimeException: Resource not found: data/maps/desert.tmx
at org.newdawn.slick.util.ResourceLoader.getResourceAsStream(ResourceLoader.java:69)
at org.newdawn.slick.tiled.TiledMap.<init>(TiledMap.java:101)
at game.Game.init(Game.java:17)
at game.Tunneler.initStatesList(Tunneler.java:37)
at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:164)
at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)
at game.Tunneler.main(Tunneler.java:29)


Here is my Game class:

package game;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.tiled.TiledMap;

public class Game extends BasicGameState{
private int stateID = -1;
private TiledMap map = null;

public Game(int stateID){
this.stateID = stateID;
}
public void init(GameContainer container, StateBasedGame game) throws SlickException{
map = new TiledMap("data/maps/desert.tmx","maps");//ERROR
}
public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException{
//map.render(0,0);
}
public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException{
}

public int getID(){return stateID;}
}


I've tried to see if anyone else has had similar problems but haven't turned up anything. I am able to load other files, so I don't believe it's a compiler issue. My menu class can load images and display them just fine. Also, the filepath is correct.

Please let me know if you have any pointers that might help me sort this out.

• I have used that very same constructor to load maps several times before. That, along with the error message of the exception, leads me to believe that it simply cannot find the file. Could you tell us where 1) the working directory that you are running the project from, 2) where the map file is. Please provide the paths as either fully qualified paths or relative to one parent folder common to both? – Jiddo Apr 29 '12 at 18:31
• Yes, here's the path "F:\workspace\Game_Tunnel\data\maps\map.tmx". It's weird because in my menu class I load an image from "F:\workspace\Game_Tunnel\data\images\bg.jpg" – mbreen Apr 29 '12 at 18:36
• That is just one of the paths. Where is your compiled source files? Where do you run the project from? (And how do you run your project? Eclipse? If so, what is the Eclipse project folder?) – Jiddo Apr 29 '12 at 18:38
• I'm using eclipse and set my workspace to the foler "workspace" – mbreen Apr 29 '12 at 18:39
• @Jiddo You should consolidate your comments into an answer so mbreen can accept it and you'll get the credit you deserve. – MichaelHouse May 1 '12 at 21:18

## 3 Answers

This answer is consolidated from my comments on the original question.

I have used that very same constructor to load maps several times before. That, along with the error message of the exception, leads me to believe that it simply cannot find the file.

Since your comments state that you can load other resources (images and sounds) but not this map that is an even stronger indication that this is the case. In Slick2D Images, Sounds, TiledMaps etc. are all loaded using this same method (the method ResourceLoader.getResourceAsStream).

Did you refresh your project in Eclipse after you created the map file? Can you see the map file from within Eclipse? Make sure you perform the refresh operation on the top of the hierarchy, on the project itself. (Right-click on the project and use refresh.) If that doesn't work, try re-creating the project. (I've never needed to do that tho.)

I have had a similar problem. but have figured out how to make sure it will load. first thing you would do is create the TiledMap object like so

public TiledMap map = null;


now you just need to tell it not to load the tilesets unless you want it to but in my game i have loaded the tilesets differently

public void load(){
map = new TiledMap("filepath", false);
}


Right click on the java app that is giving you the error message. Go to >RUN CONFIGURATION. Take a look at the MAIN CLASS line. The MAIN CLASS should be the class that has your main class method in it. If it doesn't, look at the left panel and find the main class that is calling your java app. Click on it and make sure that it is set as the main class in the MAIN CLASS line.

• I'm not sure you're answering the correct question. Changing the main class for a java application cannot be done from the running java app. Additionally, this doesn't say anything about loading tmx files. The error being presented has nothing to do with setting the main class. – MichaelHouse Feb 2 '13 at 15:34