2

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

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@BalucC the package is all uppercase, that was before I got to the code. .class file is in /WEB-INF/classes/MY directory but I don't know which jar file to check in /WEB-INF/lib
If you don't have it in a JAR, then you don't need to check JARs :) At any way, this exception is telling you that the specified class is not present in the classpath. It could be a typo in the name, a wrong file name, a wrong file extension, a wrong (re)deployment, anything. You're the only who can tell this since you have direct access to the environment.
It can't be that the problem is with the classpath, because all the other classes in the same dir load up just fine... But for now it decided to go away on its own. Let's see if it holds.
Then it was likely not correctly rebuilt/redeployed/restarted. Some servers also have a cache in tmp/work directory. You might want to cleanup it as well.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.