File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Classpath, lib directory, and lots of libraries! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Classpath, lib directory, and lots of libraries!" Watch "Classpath, lib directory, and lots of libraries!" New topic

Classpath, lib directory, and lots of libraries!

David Irwin
Ranch Hand

Joined: Mar 25, 2004
Posts: 82
I'm working on a current project that requires a long list of third party library jar files. Currently I have placed them all in a lib directory. I currently have a bat file that looks a little something like:

and so on. I find this pretty ugly and I don't like having to edit this file as I add or remove required library files from the project. Is there a way to specify that all jar files found in my lib folder are to be added to the classpath? I've tried the following:

but that doesn't work as it doesn't load any of the jar files in the lib folder. Am I missing something obvious? Is there a much better way to handle loading a large number of library jar files? Thanks.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 694
Look at the java Extension feature. It will use the jar files that are in the jre\lib\ext folder. I don't know about compiles though.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

There is no brief solution in java afaik.

You may specify a directory, containing classes, or a jar-file, containing classes.
But not directories, containing jars, containing classes.

There are several ways to work around:
a) The big, fat mama-jar:
Unjar all the jars, and create a super jar, containing all those classes.

Not very elegant, I admit.

b) Move all jars to %JAVA_HOME%/jre/lib/ext where the jvm will look automatically for jars.

Unfortunately some people report it doesn't work for them, while it works for me.

Not very elegant too.

c) Generate the classpath on the fly:

Well - you have to translate this to Windows-Scripting Language or to get the native gnu-utils for win32, but you got the idea?
ls *.jar is a brother of dir *.jar, and the 'tr'-command means: translate linefeed to Unix-Pathseparator : (Win: .

It's not very elegant too, in my opinion.
Feel free to find a better one.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
You may have good reasons for using a .bat file, but have you considered using Ant? It greatly simplifies handling classpathes, up to the point where you can specify that all jars in a directory should be used, no matter how many there are.
David Irwin
Ranch Hand

Joined: Mar 25, 2004
Posts: 82
Thanks for all the responses. I'm glad to see that it isn't something obvious that I've missed, but I'm disappointed that there isn't an elagent solution to this problem. I certainly can't be the only one with this problem. There are some good suggestions above...what have others done?

And Ulf, I do use Ant and do have a "launch" target to run my application and I should have made mention of it. To date that's the best solution I've found.

Thanks again for the replies!
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
ANT provides the solution you seek. You create a fileset in your classpath that includes all jar files. Java Jar files also have a solution: use a jar manifiest with a Class-Path setting that lists dependent jars.

Personally, blindly loading all jars in a folder is the antithesis of elegant. How often do you run a Java program that is not started from a script? Never? Probably very infrequently. I would not even put that on the list of possible java enhancements. It would cause more problems than it solves. In fact I would not even characterize it as solving a real problem.
I agree. Here's the link:
subject: Classpath, lib directory, and lots of libraries!
It's not a secret anymore!