Tomcat CLASSPATH is different from Java, and it does not include a ".". How do I change it?

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/. !

+3
3

, -, - WEB-INF/classes JAR, WEB-INF/lib. , , , .

+7

Tomcat .. , . - java -cp .

, Tomcat classpath. Tomcat , WAR (-), . - :

.
|-- WEB-INF
|   |-- classes
|   |   `-- com
|   |       `-- mycompany
|   |           `-- MyServlet.class
|   |-- lib
|   |   |-- bar.jar
|   |   `-- foo.jar
|   `-- web.xml
|-- application.jsp
|-- image.gif
`-- index.html

WEB-INF/classes - , , Tomcat . WEB-INF/classes/controllers, controllers ( , ). , :

  • controllers
  • .

, .

+7

CLASSPATH "" ".". , , . , CLASSPATH , Java 1.1 ( 1997 ) 12 .

, - . controllers ( ) WEB-INF/classes/, classes.

+2

Source: https://habr.com/ru/post/1720883/


All Articles