3

I have recently developed a game in Slick2D, i have accessed all my images directly e.g

Image i = new Image("address.png");

as opposed to using a class that will load resources or using an input stream.

I wondered if it would still be possible to load all the resources into a jar, i added the /res folder to my buildpath and used jarsplice to add my libraries and natives however the jar will not run because it cannot find the images.

Josh Jackson
  • 119
  • 1
  • 2
  • 9

1 Answers1

3
Image i = new Image("address.png");

Is looking into the root filesystem where your application is running. If you want to use the resources packed in your jarfile you must do:

Image i = new Image(getClass().getResource("/res/address.png").toURI()); // In case your Image object accepts URI as parameters

EDIT

Image i = new Image(getClass().getResource("/res/address.png").toExternalForm()); // Since your object only accept Strings
Bruno Vieira
  • 3,884
  • 1
  • 23
  • 35
  • Bruno, will the 2nd option work universally, i.e. when you are running the app out of the compiled classes (e.g. Eclipse run/debug) as well as running it out of the jar? – amphibient Oct 03 '12 at 17:46
  • Yes, it surely will. BTW, which Image object is this one? – Bruno Vieira Oct 03 '12 at 18:02
  • Thanks for the answer, however when i do this i get an error will this method work with the org.newdawn.slick.Image version of image? – Josh Jackson Oct 03 '12 at 18:16
  • THANKYOU I am a bit of a noob at exporting and this has helped alot thanks :) – Josh Jackson Oct 03 '12 at 18:22
  • 1
    Don't forget to mark the answer as correct. Otherwise stackoverflow will never know that your problems has being solved. Besides that, you earn reputation by accepting an answer (and so does who answered) – Bruno Vieira Oct 03 '12 at 18:24
  • Actually - sorry to be a trouble , but it still isnt working - i dont get an error until i run it and then i get – Josh Jackson Oct 03 '12 at 18:28
  • Exception in thread "main" java.lang.NullPointerException at Main.Menu.init(Menu.java:44) at Main.MainClass.initStatesList(MainClass.java:28) 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 Main.MainClass.main(MainClass.java:40) – Josh Jackson Oct 03 '12 at 18:29
  • Post your line 40 from your MainClass.java here – Bruno Vieira Oct 03 '12 at 18:30
  • appgc.start(); so i guess there is an issue with the entire program because this method of loading resources isn't compatible with Slick – Josh Jackson Oct 03 '12 at 18:36
  • Post Line 28 (Sorry, I mean 28 in my last comment, just got distracted) – Bruno Vieira Oct 03 '12 at 18:37
  • this.getState(menu).init(gc, this); this is where i call the class menu (where i am using this technique) – Josh Jackson Oct 03 '12 at 18:38
  • BTW, post it as another question with your whole main.java file. It seems it doesn't have to do anymore with packaged resources – Bruno Vieira Oct 03 '12 at 18:39
  • How to package resources, that are accessed directly , into a jar file - Full Source Code – Josh Jackson Oct 03 '12 at 18:50