2

I am trying to add a new class to a pre-existing web application in Tomcat6.

The class is:

public class Hello {
  public static void main(String [] args) {
   System.out.println("HELLLLLLOOOOOOO");
  }
}
  • I compiled it and put the class file in: <WEBAPP>/WEB-INF/classes
  • Restart tomcat I called it from the jsp which works with other classes in folders in the classes directory: <%@ page import="Hello" %>

It fails saying:

org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 16 in the generated java file The import Hello cannot be resolved

What am I missing?

I have also tried adding it to the catalina common/lib directory, put it external to tomcat and edited the shared.loader property in catalina.properties.

p.campbell
  • 98,673
  • 67
  • 256
  • 322
delarma
  • 33
  • 1
  • 6
  • Unrelated to the problem: do you realize that just importing the class won't invoke its `main()` method? Also, I'd suggest to learn Servlets and Javabeans before it's [too late](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files). – BalusC Nov 01 '10 at 18:38

1 Answers1

4

Put it in a package.

package com.example;

And put it in classpath accordingly:

/WEB-INF/classes/com/example/Hello.class

Finally import it as follows:

<%@ page import="com.example.Hello" %>

Packageless classes are invisible/unimportable for classes inside a package. The JSP file itself is namely implicitly been converted to a servlet class inside a servletcontainer-specific package.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555