By default, only the current working directory is included in the CLASSPATH.
But it can be configured to contain any directory on your computer.
In this sense it is no different from the PATH environment variable. Just like your OS looks at the directories in the PATH environmental variable to find a program it needs to run, the JRE looks in the CLASSPATH to find a class it needs.
CLASSPATH can be configured as a command line argument or as a CLASSPATH environment variable.
THE STRANGE THING is that CLASSPATH can be dynamically changed by the Java program that is running!!!
This leads to a phenomenon called CLASSLOADER HELL! Because when you are running Java programs inside a Web Server, it gets very hard to know what exactly the CLASSPATH is! Because different WebServers (Tomcat, Weblogic, JBoss, ..) have different ways to dynamically configure the CLASSPATH! There seems to be NO STANDARD in this regard!
If a class is not found in the CLASSPATH, you get a NoClassDefFoundError exception.
So, if we try to run the HelloWorld program form a different directory (i.e. a directory other than the one in which HelloWorld.class is located), we will get the NoClassDefFoundError exception. Because remember that by default, only the current directory is included in the CLASSPATH.
See example below:
C:\users\LavanniM\confucius>java HelloWorld
Hello World!
C:\users\LavanniM\confucius>cd ..
C:\users\LavanniM>java HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: HelloWorld. Program will exit.
C:\users\LavanniM>
3 comments:
Mahesh - Tomcat, Weblogic etc are app servers - not webservers.
Good point - WebServer vs AppServer is another confusion in Java world. There are no good definitions for a App Server. In the olden days, I believe it was used as differentiators for web servers which did and did-not support Java Apps. For example, Apache HTTP Server Vs Apache Tomcat. At this point, this differentiation is probably unnecessary!
Here is a more complete discussion:
http://www.javaworld.com/javaworld/javaqa/2002-08/01-qa-0823-appvswebserver.html
I have a better understanding of the terminology now:
Web Server is a pure HTTP server, unaware of java. For e.g. Apache and SunONE are Web Servers.
App Server is a JAVA server which can run J2EE web apps. For e.g. Tomcat and Weblogic are App servers.
Web clients send request to Web Server which then redirects it to the App Server.
Oftentimes, an App server is enhanced to have a Web Server built into it. In this mode,you do not need to have an independent Web Server. For example, Catalina is the integrated web server for Tomcat. I believe WebLogic and JBoss can also act as web servers, in addition to App servers.
TCServer from Spring Source is also an App server. It is an enhancement to Tomcat with enterprise capabilities like advanced operational capabilities, diagnostics, etc
Post a Comment