aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wildcard jars... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wildcard jars..." Watch "Wildcard jars..." New topic
Author

Wildcard jars...

Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
Hi,

Let's suppose I have some classes stored in a jar file.

If I want my application to be able to access those classes at runtime, I can achieve this by adding the jar file to the classpath.

e.g. /'myjars/myjar.jar'

Or, I can set my classpath to be '/myclasses/' and know that if I had extracted my .jar file into the myclasses directory, they would also be found at runtime.

The second option is nice because if I expand more jars under the myclasses structure then the '/myclasses/' classpath would not need to change in order to find them.

My question, finally :-), is...is it possible to somehow specify classpath as '/myjars/' and just plonk all the jars I need in that folder? So that I can add further jars in the future without having to A) change the classpath, or B) have t worry about expanding them out?

I can't seem to get it to work and have tried various combination of *, *.jar et cetera.

Cheers,

Simon.


SCJP 1.4 (93%)<br />SCJD (In progress. It can run, but it can't hide...)
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Simon,

I'm afraid the second option is not possible. For *.zip or *.jar, the classpath must end with the name of *.zip/jar file. However, for *.class files, the classpath ends with directory name that contains the *.class files.

For more information, take a look at the Sun tooldocs on classpath.

Joyce
Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
Hey Joyce,

Thanks for the input.

I suppose that I could write a method that would look in a given folder and add all the .jar files it finds there to a classpath setting...but then of course the JVM has already started and it is generally accepted that you should not change the classpath of the JVM once it is running.

I wonder if there is a mechanism in 1.4 or 1.5 that allows a classpath to be updated at runtime...

Kind regards,

Simon.
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
It seems that clsspath cannot update in runtime.

I know Java allows us to load library files during runtime, it might be possible to load the files, however, I havent tried this.

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
You can write a shell or batch script to add all your jars from a certain location to your classpath.

Snippet from a ksh script that grabs all jars/zips from a lib directory and adds them to the classpath:


Do you not have the same issue when compiling or is an IDE handling that?

Ant and other build tools can be used to build complex classpaths, not that yours sounds particularly complex.
Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
Hi Carol,

Yep, another solution thanks.

Such a shame that classpath manipulation cannot by done, by Java, in a more integrated fashion, rather than having to fiddle around, up front, before the JVM is invoked.

Anywise, I am grateful for your time, trouble and suggestion.

Cheers,

Si.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
eeek! Some horrible proposed solutions!!
What you want is a custom class loader.
Please oh please don't implement any of the hacks that have been suggested, or at least, don't let them go into production where I might see them one day (please!)


Tony Morris
Java Q&A (FAQ, Trivia)
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
All it sounded like Simon wanted to do was avoid having to edit the classpath whenever a new jar was added to a project. I suggested ways to build a classpath rather than extract all the jars into one myclasses directory.

Custom classloader seems like overkill. Or I missed the boat, but don't consider my suggestions hacks.
[ March 01, 2005: Message edited by: Carol Enderlin ]
Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
Tony,

Thanks for adding to the pool of suggestions.

Do you have a code example of how a "custom class loader" could be used to load a required class; given a known location that contains a number of jar files, one of which, indeed, contains the class.

Cheers,

Simon.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Wildcard jars...