Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Classpath, lib directory, and lots of libraries!

 
David Irwin
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 733
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
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.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic