Question

this is a windows server running Apache and Tomcat. I added a new class to an existing package and tried to reference it from a jsp page like this:

jsp:useBean id="promotion" class="MY.NEWCLASS" scope="session" 

am I missing something? Do I need to do something special to add a class to a package? Thanks in advance.

the jsp page loads fine, but when I try clicking on the button that uses this class I get uncaught exception: java.lang.ClassNotFoundException:MY.NEWCLASS

Was it helpful?

Solution

You need to ensure that the class file is present in /WEB-INF/classes/MY/NEWCLASS.class of the deployed webapplication. Or when it's in a folder /MY/NEWCLASS.class inside a JAR file with the name filename.jar, then you need to ensure that it is present in /WEB-INF/lib/filename.jar of the deployed webapplication.

Please note that the fully qualified name is case sensitive. So the name must be an exact match. Maybe you was careless with the code example in your question, but using capitals this way is not a normal naming convention. Chances exist that it is actually called my.NewClass for example. You should then declare as such in your jsp:useBean.

OTHER TIPS

This looks like you forget to deploy the new class to the server. How can you debug this?

First, you can examine the classpath. In the place where you get the exception, get the current classloader:

ClassLoader cl = getClass().getClassLoader();

If that's a URLClassLoader, ask it for it's URLs:

while( cl != null ) {
    if( cl instanceof URLClassLoader ) {
        URL[] urls = ((URLClassLoader)cl).getURLs()
        for( URL url : urls ) System.out.println(url);
    }
    cl = cl.getParent();
}

Now you can check that the output contains what you expect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top