I’ve had some trouble understanding the class loader stuff. I sort of figured it out, and now i wish to post the following as a resource to people new to Class loaders:
Actually I haven’t figured it out whatsoever and this below is related only to WebStart. When you use webstart you access things through JAR’s which are placed locally on your machine without you knowing how the computer will rename them. I think, because of that, i used Class Loaders, but they have other very important uses.
To get a brief correct explanation of the main purpose of Class Loaders read few posts below (Jeff).
Using relative or absulte paths is not enough if you want your application to work on other computers/systems. When making an application it is a very good if not necessary practice to use Class loaders. You use them so that your application (code) can locate outside resources such as images, text files and so on whether it is run on Linux, Windows or using WebStart. So whenever you have a part of your code accessing an outside file (for example, a texture JPG somewhere in you package) you need to replace it with something like the following:
String file = "package/dir/dir/space.JPG";
URL url = this.getClass().getClassLoader().getResource(file);
URLConnection conn = url.openConnection();
InputStream i = conn.getInputStream();
Image image = BitmapImage.loadImage(i, file); //for images...with Input stream you can read bytes
//OR
BufferedReader reader = new BufferedReader(new InputStreamReader(i);//for txt files
*BitmapImage is a not a standard class so ignore it here. It reads the bytes out of an InputStream
So what you have to pay attention to is the following
this.getClass().getClassLoader().getResource(file);
I’ve used other ways of getting a hold of the resources but this one worked the best for me. As i understand it, whatever is in the classpath of the class calling this line can be retrived as an URL.
So there it is. I am also beginning to understand this, but i wanted to share the information i gathered with anyone who might need it. Also this is my understanding of ClassLoaders and even though it is very shallow it helped me get my game up using WebStart.
