Tuesday, November 8, 2011

Configuring CLASSPATH for jar libraries

When you include a directory in CLASSPATH, all the .class files become available to the JRE. But... not the .jar libraries.

You have to explicitly include the jar libraries in your CLASSPATH

For example, let us use log4j in our HelloWorld - instead of printing to console ourselves, we will use log4j to do it.

* Create a /lib directory under /confucius
For example, I have:
C:\users\LavanniM\confucius\lib

* Download the log4.jar (it will be something like log4j-1.2.16.jar - with the version) from the log4j website
* Copy the jar to the \lib

Now update your HelloWorld class to use log4j

 import org.apache.log4j.Logger;  
import org.apache.log4j.BasicConfigurator;

class HelloWorld {
public static void main(String[] args) {
// Set up a simple configuration that logs on the console.
BasicConfigurator.configure();
Logger logger = Logger.getLogger(HelloWorld.class);
logger.debug("Hello World!");
}
}




* Try compiling the source code (you will get a compilation error - see below):
 C:\users\LavanniM\confucius>javac HelloWorld.java  
HelloWorld.java:1: package org.apache.log4j does not exist
import org.apache.log4j.Logger;
^
HelloWorld.java:2: package org.apache.log4j does not exist
import org.apache.log4j.BasicConfigurator;
^
HelloWorld.java:7: cannot find symbol
symbol : variable BasicConfigurator
location: class HelloWorld
BasicConfigurator.configure();
^
HelloWorld.java:8: cannot find symbol
symbol : class Logger
location: class HelloWorld
Logger logger = Logger.getLogger(HelloWorld.class);
^
HelloWorld.java:8: cannot find symbol
symbol : variable Logger
location: class HelloWorld
Logger logger = Logger.getLogger(HelloWorld.class);
^
5 errors

C:\users\LavanniM\confucius>


Easy to see why - our log4j.jar is not in the CLASSPATH.

So try adding the /lib directory to the CLASSPATH and try again - it will still fail (see below)

 C:\users\LavanniM\confucius>set CLASSPATH=C:\users\LavanniM\confucius;C:\users\LavanniM\confucius\lib;  

C:\users\LavanniM\confucius>javac HelloWorld.java
HelloWorld.java:1: package org.apache.log4j does not exist
import org.apache.log4j.Logger;
^
HelloWorld.java:2: package org.apache.log4j does not exist
import org.apache.log4j.BasicConfigurator;
^
HelloWorld.java:7: cannot find symbol
symbol : variable BasicConfigurator
location: class HelloWorld
BasicConfigurator.configure();
^
HelloWorld.java:8: cannot find symbol
symbol : class Logger
location: class HelloWorld
Logger logger = Logger.getLogger(HelloWorld.class);
^
HelloWorld.java:8: cannot find symbol
symbol : variable Logger
location: class HelloWorld
Logger logger = Logger.getLogger(HelloWorld.class);
^
5 errors

C:\users\LavanniM\confucius>


Not so easy to understand why this time - but it is because we included the /lib directory - but NOT the jar itself (this is always a big source of confusion)

So now try adding the log4j.jar in the CLASSSPATH and this time commpilation will succeed - and the program will run (see below)

 C:\users\LavanniM\confucius>set CLASSPATH=C:\users\LavanniM\confucius;C:\users\LavanniM\confucius\lib\log4j-1.2.16.jar;  

C:\users\LavanniM\confucius>javac HelloWorld.java

C:\users\LavanniM\confucius>java HelloWorld
0 [main] DEBUG HelloWorld - Hello World!

C:\users\LavanniM\confucius>

1 comment:

Anonymous said...

Same example by setting the classpath from the command line:

dattanik@DattaniK-02 /cygdrive/c/Temp/java_test
$ javac -cp ./lib HelloWorld.java
HelloWorld.java:1: package org.apache.log4j does not exist
import org.apache.log4j.Logger;
^
HelloWorld.java:2: package org.apache.log4j does not exist
import org.apache.log4j.BasicConfigurator;
^
HelloWorld.java:9: cannot find symbol
symbol : variable BasicConfigurator
location: class HelloWorld
BasicConfigurator.configure ();
^
HelloWorld.java:10: cannot find symbol
symbol : class Logger
location: class HelloWorld
Logger logger = Logger.getLogger (HelloWorld.class);
^
HelloWorld.java:10: cannot find symbol
symbol : variable Logger
location: class HelloWorld
Logger logger = Logger.getLogger (HelloWorld.class);
^
5 errors

dattanik@DattaniK-02 /cygdrive/c/Temp/java_test
$ javac -cp ./lib/apache-log4j-1.2.16/log4j-1.2.16.jar HelloWorld.java

dattanik@DattaniK-02 /cygdrive/c/Temp/java_test
$