Search code examples
javatomcatservletscompiler-errorspackaging

Packaging Servlets (symbol error on compile)


I've been building my first (somewhat large) tomcat/mysql project. Things have been going well, but now that I want to place my servlets into packages (and import classes) things don't want to compile.

Servlet Repository: C:\tomcat\webapps\web\web-inf\classes\com\coreservlets

Package name: com.coreservlets

cmd shell command(compile): (I know this classpath / compile path works)

C:\tomcat\webapps\web\web-inf\classes\com\coreservlets>

javac -classpath c:\apache\tomcat\lib\servlet-api.jar LoginHandler.java

I have two servlets in the repository (one compiles, the other doesn't). The one that doesn't compile gets this message when compiling

  LoginHandler.java:16: error: cannot find symbol
  import com.coreservlets.MyUser;
                         ^
  symbol:   class MyUser
  location: package com.coreservlets

my XML:

<servlet>
    <servlet-name>LoginHandler</servlet-name>
    <servlet-class>com.coreservlets.LoginHandler</servlet-class>
</servlet>
<servlet>
    <servlet-name>MyUser</servlet-name>
    <servlet-class>com.coreservlets.MyUser</servlet-class>
<servlet-mapping>
    <servlet-name>LoginHandler</servlet-name>
    <url-pattern>/LoginHandler</url-pattern>
</serlvet-mapping>
<servlet-mapping>
    <servlet-name>MyUser</servlet-name>
    <url-pattern>/MyUser</url-pattern>
</serlvet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>

The LoginHandler code snippit

package com.coreservlets;
import com.coreservlets.MyUser;

The MyUser code snippit

package com.coreservlets;

The MyUser.java file will compile without and trouble. Only the LoginHandler fails to compile. I see that it understands where the class MyUser is, and sees the package location... what am I doing wrong here?

*This is what I am trying to do more or less (just get servlets packaged and be able to import/export java classes/constructors: http://www.tutorialspoint.com/servlets/servlets-packaging.htm

Thanks!


Solution

  • You have problem with classpath. There are many ways to solve it, the simplest is call

    javac -classpath c:\apache\tomcat\lib\servlet-api.jar *.java
    

    BTW, more precise, but also very simple tutorial is http://www.mkyong.com/servlet/a-simple-servlet-example-write-deploy-run/