Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Build dynamically extensible applications

Find out how to build programs that you can extend dynamically -- even while they execute

  • Print
  • Feedback
Most Java programs are written in this (albeit grossly over-simplified) fashion:

  • A programmer writes, compiles, and debugs a bunch of code.
  • Then he (or, increasingly, she) distributes the completed program.


The point I'm trying to illustrate is that once the program is written, it contains all of the features it will ever have. If new features are ever necessary, the programmer must go back and edit the source code to include the new features, recompile the code, and redistribute the updated program.

The fact that most Java programs are written in this fashion is the result of Java's C and C++ heritage: Both C and C++ are statically linked languages, which means that all of the pieces that make up a C or C++ program must be present at the link step. Couple this with the fact that many Java programmers migrated from a C and/or C++ environment, and you can see why this type of programming is the norm. The thing is, in Java, we are not limited by the language in this way. Java, as you will soon see, allows programs to be dynamically extensible -- that is, to dynamically load in and execute new code, including code that wasn't in existence when the program was written. Sound far-fetched? It's not. What's more, I'm betting you use at least one program like this every day.

To make the distinction clear, I'll call programs written as described at the beginning of the article as "not dynamically extensible." Now, I don't want you to think that not being dynamically extensible is some sort of a flaw. That is not necessarily so. Some programs shouldn't be dynamically extensible. However, many programs, including most shrink-wrapped applications, would benefit users greatly if they were.

Just try to imagine a Web browser written in Java that could not load and execute Java applets, which are dynamically loaded into the browser. The thought is almost comical -- that's essentially the whole reason for writing a browser in Java. A browser, therefore, is a good example of a program that should be dynamically extensible. (Didn't I tell you that you probably used a dynamically extensible program daily?)

I bet you're wondering why this feature is important for programs other than browsers. Well for one thing, if a program is dynamically extensible, you don't need to modify the main body of source code in order to add new features. This is a great blessing for two reasons. First, editing working code can introduce bugs. Second, end users don't need access to the original source code.

Dynamic extensibility is important for another reason, as well. If the program is written correctly (and I'll show you how), you don't even have to restart the program to pick up the additions. Consider a browser once again. If it encounters an applet tag in an HTML document, it locates the applet code, loads it, and executes it without the user ever having to exit the browser. This is a good thing.

The technique I'm about to describe will work under both Java 1.1 and Java 1.0. It doesn't use JavaBeans, class introspection, or class serialization (APIs provided only with Java 1.1). Instead, it makes use of features that have been in the language and the class library since the beginning.

  • Print
  • Feedback

Resources
  • Browse the documentation for class Class online http://www.javasoft.com/products/jdk/1.0.2/api/java.lang.Class.html
  • See how dynamic extensibility is handled in TCL http://sunscript.sun.com/man/tcl8.0/TclCmd/load.htm
  • See how dynamic extensibility is handled in PERL ftp://ftp.cis.ufl.edu/pub/perl/CPAN/doc/manual/html/pod/perlmod.html
  • See how dynamic extensibility is handled in Python http://www.python.org/doc/ext/node21.html#SECTION00400000000000000000
  • Download this article and the complete source code as a gzipped tar file http://www.javaworld.com/javaworld/jw-09-1997/howto/jw-09-howto.tar.gz
  • Download this article and the complete source code as a zip file http://www.javaworld.com/javaworld/jw-09-1997/howto/jw-09-howto.zip
  • Previous How-To Java articles