Wednesday, September 22, 2004

One of my favorite things about Java: packaging both code and data in JAR files

Sure, other programming languages like Python, Lisp, and Smalltalk as faster to develop in (for the usual reasons: late binding, more interactive development, etc.). However, one feature of the Java platform that I really like is being able to easily package both compiled code and required data in a JAR file.

Typically, I serialize required runtime data to a binary file and when I create a JAR file I add the binary serialized data file as a top level entry. To read the data into memory, I use something like this:

InputStream ins =
this.getClass().getClassLoader().getResourceAsStream("my_data.ser");
ObjectInputStream p = new ObjectInputStream(ins);
Vector my_vector = (Vector) p.readObject();
// etc.

Then, I can just use the JAR file in other applications and I have both code and required data.

No comments: