Wednesday, February 27, 2008

List file class loader

Matrex is composed by several jars:
  • The Matrex jars: api, gui, fun.
  • The libraries used by Matrex, for example SWT, jfreechart.
  • The plug-ins, for example the library to connect Matrex to Matlab.
To have java loading all these jars when starting Matrex, they was listed in the classpath parameter of the java call.
Here is the typical content of a batch file starting Matrex (it uses the two variables MATREX_CP and LIB_CP):
java -Djava.library.path=swt -classpath $MATREX_CP:$LIB_CP matrex.gui.Start $1

This approach has several problems:
  • It is not readable, and it becomes less readable adding more libraries.
  • If the user wants to add a plug-in, he has to edit the batch file. This is unconfortable, because the user needs to learn the syntax of the batch file, and dangerous: if the user does a small mistake changing the batch file the program does not start.
  • you need to have one copy of this batch file for each platform in which Matrex is released (Windows, Linux, MacOSX...).

In these days I worked on a new approach. The idea is to have 3 files containing the list of jars needed to start Matrex:

  • matrex.cld containing the main Matrex jars
  • libraries.cld containing the libraries used by Matrex
  • plugis.cld containing the plug-ins (it is empty when Matrex is installed)
Here is an content of the matrex.cld file. The other two files follow the same rules:


To load these files and to load the jars listed in them, Matrex uses a custom class loader, TextFilesClassLoader.

The start class of Matrex is now called MatrexLoader.
MatrexLoader loads the main class of Matrex, Start, using TextFilesClassLoader as class loader.
Since Start all the other classes of Matrex (included the ones in the library) are loaded, directly or indirectly, from Start, they all use the same class loader used by Start, TextFilesClassLoader.

In this way it has been also possible to write a special Matrex GUI dialog, PluginsDialog, to update the content of plugins.cld:

This is much easier than to change manually the batch file.

No comments: