Variable classpath entries can help you put common libraries on the classpath without actually including them in your project. It can help you save local space, server space, and time. It's easy to learn, easy to do, and it helps force best-practice thinking along the lines of 'reuse'.
Every time you setup a new project in your Eclipse-based IDE, you likely start by including common libraries. You might import log4j or the mysql-connector jar files, for example. When you find yourself using the same libraries in most all of your projects, it's a good sign that you can optimize your time and your deployments by using variable classpath entries.
Suppose, for example, that you always import log4j into your WEB-INF/lib folder. This consumes additional space on your local machine and those of your fellow developers. It may also consume unnecessary space in your team source-code repository and on the server. The additional bloat on your WAR file can slow down the build process and consume precious time over the long-run.
Instead of actually importing common libraries, consider holding only one instance of the library on your local machine, in your source-code repository, and on your server. Using variable classpath entries will allow you to put this sole instance on your classpath so that your projects will compile without actually adding them physically into the project itself. Here's how:
Start by right-clicking on your project and selecting Properties. Then, in the Project Properties dialog (shown below), select Java Build Path.

Next, click the 'Add Variable...' button to bring up the New Variable Classpath Entry dialog as shown below.

Once you have already created a variable classpath entry, it will appear in this list and you can simply select it to add it to the classpath. We'll assume this is your first time, however, so we will need to configure a new variable. Click the 'Configure Variables...' button to bring up the Classpath Variables dialog as shown below.

Click the New... button to bring up the New Variable Entry dialog as shown below. Enter a name for the variable such as 'LOG4J', for example. Click the 'File...' button and browse to the path of the library on your local machine.

When you have given a name and path, click 'OK'. You should then see the new variable entry selected in the Classpath Variables dialog. If it is not selected, select it, and then click 'OK'. You should then also see the new variable in the New Variable Classpath Entry dialog as shown below.

Make sure the variable entry is selected and click 'OK'. The entry will then be added to the Java Build Path under the Libraries tab of your project properties as shown below.

Click 'OK'. Your project now includes the common library and should compile. The same variable can now be used in several projects. Your development team can configure a variable of the exact same name on their own local machines. On the server, you can deploy the library to a shared classpath location so that it does not have to get included in every new build.
This technique may not be ideal in every case; you have to be the judge. Be aware that if you store libraries in a shared location on the server and also include them in your deployed project, you can run into some nasty classloading errors. So, in the case of our example here, we'd want to be sure everyone was well aware that Log4j is shared on the server and should not be included in the project build.
Comments (3)
Aug 07, 2008
Anonymous says:
My understanding is that the Eclipse team is recommending moving more towards us...My understanding is that the Eclipse team is recommending moving more towards using "User Libraries" as a replacement for "Classpath Variables". User Libraries have the advantage of allowing you to add multiple jars to a single library and then specifying a single entry in your build path, whereas a Classpath Entry is a single path that would need extending for each jar, meaning multiple entries on your build path.
Aug 07, 2008
Cody Burleson says:
That is interesting and does indeed sound like a better approach overall. This i...That is interesting and does indeed sound like a better approach overall. This is not yet available in Eclipse though, right?
Oct 05, 2008
Kalairajan Swarnam says:
I havent checked the Previous version but In eclipse 3.4.1 User Libraries Option...I havent checked the Previous version but In eclipse 3.4.1 User Libraries Option is available , path to navigate to user libraries
build path --> libraries --> add library --> user library
once you create a user library you can include that in your project build path and all the jars mentioned in your user libraries will be included in the class path
Add Comment