java - Getting a directory inside a .jar -


i trying access directory inside jar file. want go through every of files inside directory itself. tried, example, using following:

url imagesdirectoryurl=getclass().getclassloader().getresource("images");  if(imagesfolderurl!=null) {     file imagesdirectory= new file(imagesdirectoryurl.getfile()); } 

if test applet, works well. once put contents jar, doesn't because of several reasons. if use code, url points outside jar, have put images directory there. if use new file(imagesdirectoryurl.touri());, doesn't work inside jar because error uri not hierarchical. sure directory exists inside jar. how supposed contents of images inside jar?

paths within jars paths, not actual directories can use them on file system. resources within particular path of jar file:

  • gain url pointing jar.
  • get inputstream url.
  • construct zipinputstream inputstream.
  • iterate each zipentry, looking matches desired path.

..will still able test applet when it's not inside jar? or have program 2 ways images?

the zipinputstream not work loose resources in directories on file system. then, recommend using build tool such ant build (compile/jar/sign etc.) applet. might take hour or write build script & check it, thereafter can build project few keystrokes , couple of seconds.

it quite annoying if have extract , sign jar if want test aplet

i'm not sure mean there. 'extract' come it? in case not clear, sand-boxed applet can load resources way, jar mentioned in archive attribute. thing might do, separate resource jar(s) applet jar. resources typically change less code, build might able take shortcuts.

i think have consider putting images seperate directory outside jar.

if mean on server, there no practical way listing of image files short of server. e.g. servers insecurely set produce html based 'file list' directory no default file (such index.html).


i have got 1 jar, in classes, images , sounds are.

ok - consider moving sounds & images separate jar. or @ least, put them in jar 'no compression'. while zip comression techniques work classes, less efficient @ compressing (otherwise compressed) media formats.

i have sign because use "preferences" class save user settings."

there alternatives preferences applets, such cookies. in case of plug-in 2 architecture applet, can launch applet (still embedded in browser) using java web start. jws offers persistenceservice. here small demo. of persistenceservice.

speaking of jws, brings me to: absolutely certain game better applet, rather app (e.g. using jframe) launched using jws?

applets give no end of stress, , jws has offered persistenceservice since introduced in java 1.2.


Comments