Adding JAR to CLASSPATH in Mac OS

15,749

You must set the jars on the classpath individually. There are ways around this though. One that I use is starting the java app with a shell script that contains something like this:

 cd $JAR_DIR
 jars=($(ls *.jar))
 JAR_PATH=""
 dir=$(pwd)
 for i in "${jars[@]}"; do
    JAR_PATH="${JAR_PATH}:$dir/$i"
 done
 CLASSPATH=$CLASSPATH:$JAR_PATH

This will work.

Share:
15,749
Tiago Veloso
Author by

Tiago Veloso

Updated on June 09, 2022

Comments

  • Tiago Veloso
    Tiago Veloso almost 2 years

    I am trying to set up the CLASSPATH for Java under Mac OS.

    Specifically I am trying to add a couple of JAR archives to it.

    If I do it like:

    ## Setting up ASM bytecode instructor library
    export CLASSPATH=$CLASSPATH:/Users/fork/Dev/ASM/lib/all/asm-all-3.3.1.jar
    

    It works fine. However, if I set it like the documentation recommends:

    ## Setting up ASM bytecode instructor library
    export CLASSPATH=$CLASSPATH:/Users/fork/Dev/ASM/lib/all/*
    

    It does not seem to work.

    The thing is that I want to add, let's say 10 jars, it sounds impractical to add one-by-one.

    Is there a solution?