Adding multiple jars to classpath on commandline

12,700

Remove the spaces from the classpath and add the current path

javac -cp jar/A.jar:jar/B.jar:jar/C.jar:jar/D.jar:. MyFile.java

Since Java 6 you can use classpath wilcards

javac -cp jar/*:. MyFile.java
Share:
12,700
user3044327
Author by

user3044327

Updated on June 11, 2022

Comments

  • user3044327
    user3044327 almost 2 years

    I'm running Ubuntu and want to execute a Java file from terminal by including multiple jar files.

    All my jars are included in tha jar folder.

    I tried

    javac -cp jar/A.jar: jar/B.jar: jar/C.jar: jar/D.jar MyFile.java
    

    I get below error.

    javac: invalid flag: jar/B.jar:
    Usage: javac <options> <source files>
    use -help for a list of possible option
    

    Can anyone guide how to use multiple jars in classpath ?

  • user3044327
    user3044327 about 10 years
    and what about if I have to execute the classfile??
  • Reimeus
    Reimeus about 10 years
    The same classpath will work for the JVM
  • user3044327
    user3044327 about 10 years
    Thanks. Works like a magic.
  • Peter
    Peter over 5 years
    Specifying application specific jars on system level is not a way to go. This is why startup scripts exist which are often a mess, but more flexible.
  • Vallerious
    Vallerious over 3 years
    What if I want to include a jar and another custom class in the same directory as my Main.java file? I tried javac -classpath ./guava-18.0.jar:. Main.java but it does not find the custom class.
  • Reimeus
    Reimeus over 3 years
    try javac -classpath ./guava-18.0.jar:. *.java