name_masked
name_masked

Reputation: 9794

Compilation error - Tomcat, jsp

All,

I just installed apache tomcat and testing the installtion did show the "Successfully installed" apache page. I did execute the HelloWorld example without any issues. So now, I created my own web application under \apache-tomcat-XXX\webapps\mine with following 2 subfolders:

\apache-tomcat-XXX\webapps\mine\classes
\apache-tomcat-XXX\webapps\mine\lib

I created a new class file HelloWorldAgain.java with following contents:

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * My test servlet
 *
 * @author Liz Warner
 */

public class Hi extends HttpServlet {

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head>");
        out.println("<title>Hola</title>");
        out.println("</head>");
        out.println("<body bgcolor=\"white\">");
        out.println("<h1> Hi </h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

However, when I compile the code from command line, I get errors related to

> Hi.java:4: package javax.servlet does not exist
import javax.servlet.*;
^
Hi.java:5: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
Hi.java:13: cannot find symbol
symbol: class HttpServlet
public class Hi extends HttpServlet {
                        ^
Hi.java:15: cannot find symbol
symbol  : class HttpServletRequest
location: class Hi
    public void doGet(HttpServletRequest request,
                      ^
Hi.java:16: cannot find symbol
symbol  : class HttpServletResponse
location: class Hi
                      HttpServletResponse response)
                      ^
Hi.java:17: cannot find symbol
symbol  : class ServletException
location: class Hi
        throws IOException, ServletException

I have set the following environement (Windows XP env.) variables:

JAVA_HOME: C:\Program Files\Java\jdk1.6.0_21 CATALINA_HOME: C:\Apache Tomcat\apache-tomcat-7.0.22

Upvotes: 0

Views: 1565

Answers (1)

Sean Owen
Sean Owen

Reputation: 66866

Your Java class uses other Java classes that are not part of Java SE ("normal" or "desktop" Java) -- the Java EE classes in javax.servlet for example. So, the compiler doesn't know about them unless you tell it where to look for those classes. You need to find something like javaee.jar or servlet.jar -- Tomcat ought to contain them in its libs/ dir, though I forget exactly what they are called.

Then you add them to your compile command with javac -cp path/to/javaee.jar ... for example.

This is really nothing specific to Tomcat or J2EE, it's just basic Java compiling.

Upvotes: 2

Related Questions