How to automatically include all *.jars in a dir tree WITHOUT explicit CLASSPATH assignment ?

L

Lars Willich

Assume I want to use temporarily a java tool like Ant or a server or whatever which consists
of a lot of necessary *.jar files.

I want to use these *.jars in my own program. Ok, I can start now to search all the dozends of
*.jars and append them individually to the CLASSPATH var string.
For a short term application test this is rather uncomfortable.

Isn't there a "collect&include all" option/flag in Java which automatcially scans
a whole directory and appedn automatically all found *.jars to the current CLASSPATH ?

Lars
 
R

Roedy Green

Isn't there a "collect&include all" option/flag in Java which automatcially scans
a whole directory and appedn automatically all found *.jars to the current CLASSPATH ?

There are some tools that you may find useful.

1. the ext directory. See
http://mindprod.com/jgloss/ext.html#EXTDIRECTORY
Any jar put there will automatically be on the classpath.

2. genjar. It will automatically include just classes needed.
see http://mindprod.com/jgloss/genjar.html

3. ant scripts of this form:

<!-- extensions we distribute. Case sensitive! -->
<property name="distributable.exts"
value="**/*.au,**/*.bat,**/*.btm,**/*.c*,**/*.class,**/*.dll,**/*.gif,**/*.h,**/*.hpp,**/*.htm,**/*.html,**/*.jar,**/*.ico,**/*.ION,**/*.ion,**/*.java,**/*.jnlp,**/*.jpg,**/*.look,**/*.png,**/*.properties,**/*.ser,**/*.txt,**/*.use,**/*.xml"
/>


<zip destfile="${zip.file}" duplicate="preserve" filesonly="true">
<zipfileset dir="${package.dir}" prefix="${package.dir}"
includes="${distributable.exts}" />
<zipfileset dir="com/mindprod/common11"
prefix="com/mindprod/common11" includes="${distributable.exts}" />
</zip>
 
K

Karsten Wutzke

Assume I want to use temporarily a java tool like Ant or a server or whatever which consists
of a lot of necessary *.jar files.

I want to use these *.jars in my own program. Ok, I can start now to search all the dozends of
*.jars and append them individually to the CLASSPATH var string.
For a short term application test this is rather uncomfortable.

Isn't there a "collect&include all" option/flag in Java which automatcially scans
a whole directory and appedn automatically all found *.jars to the current CLASSPATH ?

Lars

There isn't. This is why Ant was created. Why not use Ant and a veeery
simple build.xml to scan Ant's or any other dirs jars?

Karsten
 
V

voorth

Assume I want to use temporarily a java tool like Ant or a server or whatever which consists
of a lot of necessary *.jar files.

I want to use these *.jars in my own program. Ok, I can start now to search all the dozends of
*.jars and append them individually to the CLASSPATH var string.
For a short term application test this is rather uncomfortable.

Isn't there a "collect&include all" option/flag in Java which automatcially scans
a whole directory and appedn automatically all found *.jars to the current CLASSPATH ?

in your Ant build file, add the following element:
<path id="my.path">
<path location="path/to/onelib.jar"/>
<path location="path/to/anotherlib.jar"/>
...
</path>

You can now add "my.path" to your <java> targets classpath.
 
K

Kevin Sandal

Assuming all your JARs are in a sub-directory like "lib", you can use a
command line of:
java.exe -cp MyCoolApp.jar;.;.\lib

Kevin Sandal
 
M

Mark Rafn

Kevin Sandal said:
Assuming all your JARs are in a sub-directory like "lib", you can use a
command line of:
java.exe -cp MyCoolApp.jar;.;.\lib

What version of java.exe is this? Every version I've looked at requires every
jar to be listed - directories can contain class files, not jars.

I know of no built-in classloader that allows a directory of multiple
jarfiles to be specified in a classpath. It's fairly easy to write one,
though.
 
K

Kevin Sandal

Mark Rafn said:
What version of java.exe is this? Every version I've looked at requires
every
jar to be listed - directories can contain class files, not jars.

I know of no built-in classloader that allows a directory of multiple
jarfiles to be specified in a classpath. It's fairly easy to write one,
though.

Works with
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_12-b04)
just fine.

Kevin
 
L

Lew

Works with
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_12-b04)
just fine.

According to Sun's docs, to get all the JARs requires a "*" in the path:
As a special convenience, a class path element containing a basename of * is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR (a java program cannot tell the difference between the two invocations).

The entry at
<http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html>
says the same.

I wonder why you didn't need an asterisk.
 
S

Snyke

This isn't hard to build into a shell script either.

That's right in TCSH:

-=-
setenv LIB_DIR "lib"
setenv CP "."
foreach f ($LIB_DIR/*.jar)
setenv CP $CP:$f
end
-=-

And at the end $CP holds your newly built classpath ^^

Regards,
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top