'javac' command - 2 minute tutorial

Setting Classpath : Option for setting classpath for compiling a java program

Let's understand the meaning of classpath.

If your java source code depends on some other classes (say log4j or gson etc.), then in order to compile your code you need to provide these log4j jars or gson jars to the javac command. For this, add the jar in the classpath of the javac comamnd by using its options -cp or -classpath .

In the example in the image below, the HelloWorld.java class depends on gson-1.7.1.jar. The gson jar is in the lib directory. So the complete command to compile is javac -classpath lib/gson-1.7.1.jar HelloWorld.java .