66

This will be a really simple freebie for a bash guru:

Question

Using bash, how do you make a classpath out of all files in a directory?


Details

Given a directory:

LIB=/path/to/project/dir/lib

that contains nothing but *.jar files such as:

junit-4.8.1.jar
jurt-3.2.1.jar
log4j-1.2.16.jar
mockito-all-1.8.5.jar

I need to create a colon-separated classpath variable in the form:

CLASSPATH=/path/to/project/dir/lib/junit-4.8.1.jar:/path/to/project/dir/lib/jurt-3.2.1.jar:/path/to/project/dir/lib/log4j-1.2.16.jar:/path/to/project/dir/lib/mockito-all-1.8.5.jar

Some seudo-code that nearly expresses the logic I'm looking for would be along the lines of:

for( each file in directory ) {
   classpath = classpath + ":" + LIB + file.name
}

What is a simple way to accomplish this via bash script?

peterh
  • 11,875
  • 18
  • 85
  • 108
gMale
  • 17,147
  • 17
  • 91
  • 116

4 Answers4

124

New Answer
(October 2012)

There's no need to manually build the classpath list. Java supports a convenient wildcard syntax for directories containing jar files.

java -cp "$LIB/*"

(Notice that the * is inside the quotes.)

Explanation from man java:

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).

For example, if directory foo contains a.jar and b.JAR, then the class path element foo/* is expanded to a A.jar:b.JAR, except that the order of jar files is unspecified. All jar files in the specified directory, even hidden ones, are included in the list. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory. The CLASSPATH environment variable, where defined, will be similarly expanded. Any classpath wildcard expansion occurs before the Java virtual machine is started — no Java program will ever see unexpanded wildcards except by querying the environment.


Old Answer

Good

Simple but not perfect solution:

CLASSPATH=$(echo "$LIB"/*.jar | tr ' ' ':')

There's a slight flaw in that this will not handle file names with spaces correctly. If that matters try this slightly more complicated version:

Better

CLASSPATH=$(find "$LIB" -name '*.jar' -printf '%p:' | sed 's/:$//')

This only works if your find command supports -printf (as GNU find does).

If you don't have GNU find, as on Mac OS X, you can use xargs instead:

CLASSPATH=$(find "." -name '*.jar' | xargs echo | tr ' ' ':')

Best?

Another (weirder) way to do it is to change the field separator variable $IFS. This is very strange-looking but will behave well with all file names and uses only shell built-ins.

CLASSPATH=$(JARS=("$LIB"/*.jar); IFS=:; echo "${JARS[*]}")

Explanation:

  1. JARS is set to an array of file names.
  2. IFS is changed to :.
  3. The array is echoed, and $IFS is used as the separator between array entries. Meaning the file names are printed with colons between them.

All of this is done in a sub-shell so the change to $IFS isn't permanent (which would be baaaad).

Community
  • 1
  • 1
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Thanks for your response. The simple version works perfectly. The *better* solution fails, for me, because *printf* is not a valid option for my find command (maybe I need to update bash? using 3.2.48 on this machine). The *best* option worked perfectly. Of course, neither the 'good' nor the 'best' option escape the spaces in file names, which is fine for me since there are none. – gMale Jan 19 '11 at 15:14
  • 1
    @gmale - The better and best options will accommodate spaces if you also quote CLASSPATH when you use it, as in `java -cp "$CLASSPATH"` – John Kugelman Jan 19 '11 at 16:49
  • The "Best" is indeed best. The "better" one doesn't handle jar file names with a space. – David Smiley Oct 05 '12 at 18:11
  • I think the most idiomatic solution is `CLASSPATH=$(find "$LIB" -name '*.jar'|paste -sd: -)`. Then you don't need this "weird" array bashism or GNU `find`. (I use an explicit `-` input parameter here to make it also work for BSD `paste`) – David Ongaro Jun 26 '14 at 04:27
  • If you don't want `CLASSPATH` set if the `$LIB` directory contains no jars, you can first run `shopt -s nullglob`. – erwaman Sep 03 '15 at 23:52
  • Can I use the same way if I have multiple directories having jar files in them ? – Akash Mahajan Feb 05 '16 at 08:09
  • @AkashMahajan Sure. Write `java -cp 'lib1/*:lib2/*:lib3/*'`. – John Kugelman Feb 05 '16 at 14:43
9
for i in $LIB/*.jar; do
    CLASSPATH=$CLASSPATH:$i
done
CLASSPATH=`echo $CLASSPATH | cut -c2-`
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
biziclop
  • 48,926
  • 12
  • 77
  • 104
  • 2
    Thanks for your response. I cut/paste this and it ran perfectly. It's a good example of using a for loop to accomplish something like this. – gMale Jan 19 '11 at 15:19
  • What does the `-c2-` param to cut do? I thought it might be `-c2 -` squished together, but that gives different results. If I omit the cut altogether I get the same results. What's it there for? – Pod Jul 10 '18 at 15:30
  • This is a good answer for non-bash shell environments that might not have `xargs` or the `-printf` option on `find`. – Pod Jul 11 '18 at 09:33
2

Here's another variation:

printf -v CLASSPATH "$LIB/%s:" *.jar; CLASSPATH=${CLASSPATH%:}

printf -v is somewhat like sprintf. The brace expansion removes the extra colon from the end.

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • Needs to be `printf -v CLASSPATH "$LIB/%s" *.jar`. Good call on the trailing colon, I had to look it up to see if it makes a difference and apparently it does, it's a valid NUL directory which causes the current directory to be searched (which may or may not be desirable). – SiegeX Jan 19 '11 at 02:02
  • @SeigeX: I made the change you suggested. I originally based my answer on "all files in a directory" and "contains nothing but *.jar files" in the question, but it pays to be selective. – Dennis Williamson Jan 19 '11 at 02:07
  • Thanks for your response. When I cut/paste & run this, I get a classpath delimited by spaces, instead of colons. I have no idea why. Other than that, it works well. – gMale Jan 19 '11 at 15:18
  • @gmale: I'm not able to reproduce that. Check to see if `IFS` has a value other than its default (space tab newline), but that shouldn't affect this. If you do `printf "%s:\n" *.jar` do you see any spaces? What about `printf '[%s]\n' "$LIB"`? – Dennis Williamson Jan 19 '11 at 15:41
0

Bestest

Everything is better with awk =)

CLASSPATH=$(awk '$0=lib"/"FILENAME' ORS=":" lib=$LIB *.jar)
Community
  • 1
  • 1
SiegeX
  • 135,741
  • 24
  • 144
  • 154
  • Thanks for taking the time to answer. I cut/paste that in and it generates the error `awk: can't open file *.jar`. So I removed the *.jar (since all files are jars in the lib directory) and the script just hangs as though awaiting input. I tried several other tweaks but I don't know enough about awk to fix it. – gMale Jan 19 '11 at 15:03