Compile Java with External Jar Files at Command Line

If you want to compile Java files at the command line, you use the javac command.

javac ../Java/*.java

This will create a .class file for every .java file that could be compiled.

But let’s say you are referencing one of many external Java libraries that are packaged as .jar files. You probably don’t want to just stick the .jar files in the same directory as the .java files. That would be sort of messy. In my case, I’m putting those in a sub-directory called lib. But the Java compiler doesn’t always know where to look for those .jar files. Below is one simple way to tell it.

javac -Djava.ext.dirs ../Java/lib ../Java/*.java

One Response to “Compile Java with External Jar Files at Command Line”

  1. Since Java 6, it’s simpler to use (and shorter to write!) Class-Path Wildcards instead:

    javac -cp ../Java:../lib/* *.java

    PS: On Windows one has to replace : with ;

Leave a Reply