Sunday, August 9, 2015

Mastering the Java CLASSPATH

It's important to understand how the java compiler figures out or searches for all the dependent classes it needs to compile a java file it needs to. I won't waste time in writing something that has already been explained beautifully in this article. So go ahead and read this article till you understand it. 

Some important take-aways:  

  • Java compiler and JVM both use the same technique to figure out the dependent classes they need, the only difference is javac compiler can compile the dependent java file too into a class (to use it) if it has not already done. 
  • javac -classpath ""                    : This will wipe out the system classpath that you have set on your computer and as we've given an empty string that means the compiler has no way to reference any other class to help build 
  • The java compiler and run-time can search for classes not only in separate files, but also in `JAR' archives. But you've to give the complete path to the .jar file and not only upto the directory in which that .jar file is stored. 
  • Usage of system CLASSPATH variable and how to add it to the javac command in case you wanna add something more to what the system CLASSPATH variable has. 

Understanding of how javac and JVM try to figure out the dependent classes is an important understanding to resolve the famous ClassNotFound exceptions.