Including all the jars in a directory within the Java classpath

前端 未结 24 3611
鱼传尺愫
鱼传尺愫 2020-11-21 04:25

Is there a way to include all the jar files within a directory in the classpath?

I\'m trying java -classpath lib/*.jar:. my.package.Program and it is no

相关标签:
24条回答
  • 2020-11-21 05:16

    You need to add them all separately. Alternatively, if you really need to just specify a directory, you can unjar everything into one dir and add that to your classpath. I don't recommend this approach however as you risk bizarre problems in classpath versioning and unmanagability.

    0 讨论(0)
  • 2020-11-21 05:17

    My solution on Ubuntu 10.04 using java-sun 1.6.0_24 having all jars in "lib" directory:

    java -cp .:lib/* my.main.Class

    If this fails, the following command should work (prints out all *.jars in lib directory to the classpath param)

    java -cp $(for i in lib/*.jar ; do echo -n $i: ; done). my.main.Class
    0 讨论(0)
  • 2020-11-21 05:18

    Correct:

    java -classpath "lib/*:." my.package.Program
    

    Incorrect:

    java -classpath "lib/a*.jar:." my.package.Program
    java -classpath "lib/a*:."     my.package.Program
    java -classpath "lib/*.jar:."  my.package.Program
    java -classpath  lib/*:.       my.package.Program
    
    0 讨论(0)
  • 2020-11-21 05:18

    If you are using Java 6, then you can use wildcards in the classpath.

    Now it is possible to use wildcards in classpath definition:

    javac -cp libs/* -verbose -encoding UTF-8 src/mypackage/*.java  -d build/classes
    

    Ref: http://www.rekk.de/bloggy/2008/add-all-jars-in-a-directory-to-classpath-with-java-se-6-using-wildcards/

    0 讨论(0)
  • 2020-11-21 05:18

    Think of a jar file as the root of a directory structure. Yes, you need to add them all separately.

    0 讨论(0)
  • 2020-11-21 05:18

    Set the classpath in a way suitable multiple jars and current directory's class files.

    CLASSPATH=${ORACLE_HOME}/jdbc/lib/ojdbc6.jar:${ORACLE_HOME}/jdbc/lib/ojdbc14.jar:${ORACLE_HOME}/jdbc/lib/nls_charset12.jar; 
    CLASSPATH=$CLASSPATH:/export/home/gs806e/tops/jconn2.jar:.;
    export CLASSPATH
    
    0 讨论(0)
提交回复
热议问题