Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3793510
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:47:12+00:00 2026-05-19T12:47:12+00:00

This will be a really simple freebie for a bash guru: Question Using bash,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T12:47:13+00:00Added an answer on May 19, 2026 at 12:47 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This will probably seem like a really simple question, and I am quite confused
I know this will really turn out to be simple, but my brain is
I know this is probably a really simple question but I'm having a brain
The question is really simple: should a path to a directory always contain a
Im sure this will be a simple one but have a project that started
I know this will be a difficult question, so I am not necessarily looking
this sounds really simple and stupid..but I'm having a hard time removing the content
I want to use a temp directory that will be unique to this build.
This will hopefully be an easy one. I have an F# project (latest F#
This will require a little setup. Trust me that this is for a good

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.