-   Linux - Server (
-   -   Tomcat 7.0.8 not finding my packaged classes. Please help. (

arashi256 03-11-2011 06:12 AM

Tomcat 7.0.8 not finding my packaged classes. Please help.
I'm trying to set up Apache Tomcat with virtual hosting. I've used Caucho Resin before but not Tomcat...and I've run into what appears to be (from Google at least) a rather common problem with Tomcat in that any properly packaged classes under WEB-INF of the default virtual host or the named host are not found and Tomcat complains of a class that "cannot be resolved to a type".

I researched this error on Google and the consensus seems to be that: -

- You need properly packaged classes as classes in the default package are no longer allowed. I have done this.

- You need to either fully declare the packaged class to make an instance of it within the JSP or include the via an import statement at the top of the JSP. I have done this.

- In the web.xml for the Tomcat server instance (under $CATALINA_HOME/conf/web.xml) you need to set the servlet mapping for JSPs fork init-param to "true". I have done this also.

Yet the problem persists and I have found no solution. So I can only assume I'm missing something obvious and fundamental. My configuration is as follows: -

I have two virtual hosts - one as the default virtual host and another as a named virtual host.

$CATALINA_HOME/webapps/localhost <--- Default host.
$CATALINA_HOME/webapps/ <--- Named host. The logic being that the URL IP address of the server will hit the default webapp and the URL hostname with hit the named webapp. This appears to work with just JSPs.

My $CATALINA_HOME/conf/server.xml looks like this: -


<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
        <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
        <Listener className="org.apache.catalina.core.JasperListener" />
        <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
        <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
        <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

                <Resource name="UserDatabase" auth="Container"
                description="User database that can be updated and saved"
                pathname="conf/tomcat-users.xml" />

        <Service name="Catalina">
                <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="443" />
                <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
                <Engine name="Catalina" defaultHost="localhost">
                        <Realm className="org.apache.catalina.realm.LockOutRealm">
                                <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>

                        <Host name="localhost"  appBase="webapps/localhost" unpackWARs="true" autoDeploy="true">
                                <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                                prefix="localhost_access_log." suffix=".txt"
                                pattern="%h %l %u %t &quot;%r&quot; %s %b" resolveHosts="false"/>
                        <Host name="" debug="0" appBase="webapps/" unpackWARs="true" autoDeploy="true">
                                <Logger className="org.apache.catalina.logger.FileLogger" directory="logs"  prefix="\_log." suffix=".txt" timestamp="true"/>

Under $CATALINA_HOME/conf/Catalina/localhost/ROOT.xml I have: -


<?xml version='1.0' encoding='utf-8'?>
<Context displayName="localhost" docBase="" path="" workDir="work/Catalina/localhost/_">

and under $CATALINA_HOME/conf/Catalina/ have: -


<?xml version='1.0' encoding='utf-8'?>
<Context displayName="$host" docBase="" path="" workDir="work/Catalina/">

With simple JSPs with no external classes under WEB-INF, this configuration works fine. But as soon as I added a class to the default webapp's index.jsp, Tomcat couldn't compile the index.jsp because it couldn't find the class I was trying to instance.

I created a simple class with one method that simply returns a String. It is held under $CATALINA_HOME/webapps/localhost/WEB-INF/classes/uk/co/f1x2games/PrintString.class and is simply: -



public class PrintString {
        private String theString = "This is a returned string. Wowie.";

        public PrintString() {}

        public String getString() {
                return theString;

Notice that it is in a package. I then added it to the default virtual host's index.jsp like this: -


<%@page language='java' import="*"%>
<title>DEFAULT Not yet configured</title>
<p>This virtual server DEFAULT is not yet configured.</p>
<% myPrintString = new;

The default host's /WEB-INF/web.xml is more or less empty at the moment with: -


<?xml version="1.0" encoding="ISO-8859-1"?>

So when hitting the IP address of the server, Tomcat attempts to compile the index.jsp but cannot because it cannot resolve to a type with the error: -

An error occurred at line: 10 in the jsp file: /index.jsp cannot be resolved to a type
7: <p>This virtual server DEFAULT is not yet configured.</p>
8: <p>
9: <%
10: myPrintString = new;
11: %>
12: </p>
13: </body>

I gather this error is to do with Tomcat not being able to find the class it's trying to instance and the only advice is that given at the top of this post.

What am I missing here? I can't *not* use classes in a webapp....:D

Help please?

cheesus 03-19-2011 07:19 AM

as the class file location.
(you can't use the classes of one webapp (localhost) in another (

but this is all very confusing.

in the end, you will be better off by using a ant script to build a .war file, like

Cheers, Tom.

All times are GMT -5. The time now is 12:37 PM.