Webserver online again

By Danny Arends in Website
Posted at: 19 Apr 2011, 18:44, last edited: 09 May 2013, 12:34

Well unfortunately there is no internet, my provider XS4ALL has got some problem keeping the connection alive.

However... My home web server is still working, though unavailable to everyone else, its my last bit of internet I got left.

Well I promised Joeri to blog about the recent 'feature' that was  harassing me in Java, a difference in resource files (e.g. png, jpg, xml) from JAR loading. It was the Classloader object which had some weird / unexpected behavior. Let me explain by a little example:

Imagine we have directory ('objects') in our Java source tree (eg. 'nl.dannyarends.www') holding objects (xml, png, etc). When using an editor like Eclipse we can easily load those object from a class by adding a function like:

public boolean loadResourceInSourceTree_WRONG(){
  File f = new File(getClass().getResource('/objects'));
  if(f.isDirectory()){
    for(File f : f.listFiles()){ 
      // Do stuff with the files 
      // Recurse into directories
    }
  }
  return true;
}

This looks like a clean cut deal simple... elegant... understandable...

However this way of approaching the files doesn't work when you try the same approach when from a .JAR file. Some Java Ant / Jar versions can build jars that support loading object this way (I assume from posts at forums). And of course its bad practice in general to load 
resources this way, because it 'assumes' a lot. A simple example where it goes wrong: Extend this class in another part of your java source tree (e.g. nl.dannyarends.game) and loading the resources fails.

To load resources from a Jar use the jarEntry object, its not to hard to find nice examples of good resource loaders. but in practice just iterate through the whole thing (and build an index).

JarInputStream jarFile = new JarInputStream(new FileInputStream('data.jar'));
JarEntry jarEntry 
do{ 
  JarEntry = jarFile.getNextJarEntry();
  //Do stuff here !
while(jarEntry != null);


I plan on separating the Applet I have into multiple parts, and use a generator to generate a UI.jar, GAME.jar, MAPS.jar, and so forth. Because I want to have them shared between client and server. The loader will be designed in such a way that it will use a fall over mechanism for missing .Jar files. On a down side doing that would mean all the objects within a Jar need to be self contained, except for the framework package. So package hell again...

Well thats all folks for a no internet evening...

Ps. No internet and JavaScript V8 & Node.js on Cloud9.ide... Gotta Love Notepad++

Last modified: 09 May 2013, 12:34 | Edit