I spent hours trying to figure out why I was getting java.lang.NoClassDefFoundError, and I narrowed down the reason for Tomcat's way.
I used the code below to see what the path variables hold:
out.println("Classpath: '" + System.getProperty( "java.class.path" ) + "'" );
out.println("Ext dirs: '" + System.getProperty( "java.ext.dirs" ) + "'" );
out.println("Library path: '" + System.getProperty( "java.library.path" ) + "'" );
out.println("Path separator: '" + System.getProperty( "path.separator" ) + "'" );
And the result:
Classpath: ': /usr/local/tomcat/bin/bootstrap.jar'
Ext dirs: '/usr/lib/jvm/java-6-sun-1.6.0.16/jre/lib/ext:/usr/java/packages/lib/ext'
Library path: '/usr/lib/jvm/java-6-sun-1.6.0.16/jre/lib/i386/server:/usr/lib/jvm/java-6-sun-1.6.0.16/jre/lib/ i386: /usr/lib/jvm/java-6-sun-1.6.0.16/jre /../ lib / i386: / usr / java / packages / lib / i386: / lib: / usr / lib '
Path separator: ':'
As you can see, Classpath does not start with "." as expected, and I believe that why my program cannot find the classes that I import from subdirectories in my web application.
To find out where the classpath is set, I did grep -R bootstrap.jar /usr/local/tomcat/and came to this: CLASSPATH="$CLASSPATH":"$CATALINA_HOME"/bin/bootstrap.jar(in the file /usr/local/tomcat/bin/catalina.sh)
This leads me to believe that for some reason $ CLASSPATH is empty here. However echo $CLASSPATHreturns successfully.:/usr/lib/jvm/java-6-sun/bin:/usr/local/tomcat/lib/servlet-api.jar
Can someone help solve the problem here?
EDIT: all of my servlet files are in WEB-INF/classes/controllers/, and the libraries I'm trying to load are class files in subdirectories. For example, if it ClassName.classis located in the WEB-INF / classes / controllerlers / packagename / directory, I add package packagenameto the beginning ClassName.javaand I import it using import packagename.*in someServlet.java.
EDIT2: . , , , . , classes/controllers/ classes/. !