This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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. Curious?
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.
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.
Joined: Mar 25, 2004
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.
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.