java -classpath *.jar

It’s quite annoying that you cannot use wildcards in the -classpath command line option to java and javac. Quite often you want to include all .jar files in one directory.

Here is a way to get that effect:

java -classpath `echo lib/*.jar | sed -e “s/ /:/g”` org.foo.MyApp

You can even include all .jar files in a whole hierarchy of directories:

java -classpath `find repository -name *.jar -printf %p:` org.foo.MyApp

In general it’s better to use Ant or Maven for compiling and Java Service Wrapper for running, they have built-in support for wildcards. But they require some upfront setup, so my solution is useful for ad-hoc usage.

This works in Linux and probably in Solaris and other UNIX systems. If you use Windows, you need to install cygwin or similar.

This entry was posted in Java. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



(this is a captcha)