ClassNotFoundException in java

java.lang.ClassNotFoundException occurs when your JVM can not find a required class within your application. The most common cause of this exception is missing required jar's in your project.

If you got this exception in Eclipse you should check your project settings. (Right click on your project , choose BuildPath and Configure BuildPath. Find Libraries tab and add required jars or libraries which are bunch of jars)

If you got this exception in command line first be sure to run your your class in correct directory. For example you have a test.Tester class. Since each package is in a folder, you run your class in upper directory of the test in command prompt.

If your class file is :

then you should run :
$cd /usr/home/youruser
$java test.Tester

If you use some jars or some other class files from other places , check your classpath. You can fix this in 2 ways.

1. You can add your jars folder to CLASSPATH environment variable of your Operating System.

On windows right-click to my computer -> Properties -> Advanced -> Environment variables. Once you add your folder , you can check it by opening new command window and type echo %CLASSPATH

On Linux you add export CLASSPATH="YourClasspath" to your .bash_profile or .bashrc file in your home directory. Here is a good explaination of those files.

2. You can override the classpath in Java.

For example , you have your jars or classes on /usr/home/youruser/javalib1 and /usr/home/youruser/javalib2

$cd /usr/home/youruser
$java -cp /usr/home/youruser/javalib1:/usr/home/youruser/javalib2 test.Tester

Current directory "." is in classpath by default for any jvm and jvm looks for directories in the order they listed in cp or CLASSPATH environment variable.

If you get this error in Tomcat , look for or catalina.bat(windows) and add your classes or jars to CLASSPATH variable of Tomcat.

Hope this helps , thanks for your comments and contribution.


Popular posts from this blog

Get Remote IP Address In Axis SOAP Server

Escape HTML and XML in Java