Hello Guest

Applet - get Files from jar

  • 9 Replies
  • 11111 Views
Applet - get Files from jar
« on: February 02, 2010, 18:00:45 »
Heyho,

I'm running my game as an applet but i'm stuck at loading the textures.
I'd like to have the image files in another jar (res.jar) file which is in the classpath.
Quote
<param name="al_jars" value="res.jar, slick.jar, App.jar, lwjgl.jar.pack.lzma, jinput.jar.pack.lzma, lwjgl_util.jar.pack.lzma">

Now i wanted to load those textures but i can't find the right way to access them, everything i tried resulted in an FileNotFoundException.
The content of res.jar looks something like this:
Quote
data
  L graphics
      L ship.png
      L enemy.png
      L bullet.png

I'm using slick's TextureLoader so i need a string pointing to the files (if it's even possible).

Regards
Dragbone

*

Offline kappa

  • *****
  • 1313
Re: Applet - get Files from jar
« Reply #1 on: February 02, 2010, 19:28:27 »
it does work (with slick) as i've done used it many times.

you sure res.jar contains the files? (have you manually opened the jar with a zip viewer program?)

you should also consider using slicks ResourceLocator with the TextureLoader

Re: Applet - get Files from jar
« Reply #2 on: February 02, 2010, 19:59:10 »
Yup, all the files are where they should be, but i don't know how i have to adress them.
Do you have any working example or can you give me some pointers on how to do it? (Java is new to me but i'm eager to learn ;-) )

*

Offline kappa

  • *****
  • 1313
Re: Applet - get Files from jar
« Reply #3 on: February 02, 2010, 20:01:14 »
if you can upload a simple test case applet + source, could help point you in the right direction.

Re: Applet - get Files from jar
« Reply #4 on: February 02, 2010, 20:31:20 »

*

Offline kappa

  • *****
  • 1313
Re: Applet - get Files from jar
« Reply #5 on: February 02, 2010, 20:38:57 »
ahh, can't believe I didn't spot this earlier.

you can't use FileInputStream to read files from a jar file.

*

Offline kappa

  • *****
  • 1313
Re: Applet - get Files from jar
« Reply #6 on: February 02, 2010, 20:41:41 »
you must do something like

Code: [Select]
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResource("data/wall.png"));

btw the import from ResourceLoader is

import org.newdawn.slick.util.ResourceLoader;

Re: Applet - get Files from jar
« Reply #7 on: February 02, 2010, 20:59:39 »
If i use
Code: [Select]
this.tex = TextureLoader.getTexture(getImageExtension(fileName),
ResourceLoader.getResource(fileName).openStream());
i get this AccessControlException:
Quote
Exception in thread "Thread-19" java.security.AccessControlException: access denied (java.io.FilePermission .\data\graphics\tr.png read)
   at java.security.AccessControlContext.checkPermission(Unknown Source)
   at java.security.AccessController.checkPermission(Unknown Source)
   at java.lang.SecurityManager.checkPermission(Unknown Source)
   at java.lang.SecurityManager.checkRead(Unknown Source)
   at java.io.File.exists(Unknown Source)
   at org.newdawn.slick.util.FileSystemLocation.getResource(FileSystemLocation.java:33)
   at org.newdawn.slick.util.ResourceLoader.getResource(ResourceLoader.java:87)
   at game.Sprite.<init>(Unknown Source)
   at game.App.gameLoop(Unknown Source)
   at game.App$1.run(Unknown Source)
The jar is signed, so that shouldn't be the problem...

*

Offline kappa

  • *****
  • 1313
Re: Applet - get Files from jar
« Reply #8 on: February 02, 2010, 21:05:14 »
also don't use backslash as that is windows only, always use forward slash as that will work everywhere, and remove the initial slash, you shouldn't need it, so it should read as

"data/graphics/tr.png"

Re: Applet - get Files from jar
« Reply #9 on: February 02, 2010, 21:20:36 »
*dang*
it was the initial slash (it was a slash, java seems to translate them into backslashes when run on windows)

it's working now, thanks a lot :D

another day wasted on something trivial^^
« Last Edit: February 07, 2010, 22:23:19 by Dragbone »