Error: package javax.Servlet does not exist import javax.servlet. *

Asked

Viewed 2,947 times

1

I created the directories correctly, copied the code and now does not compile and returns this error. I am compiling by the terminal using javac.

http://pastebin.com/WLaGQit3

package com.example.web;

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

    public class BeerSelect  extends HttpServlet {

        public void doPost ( HttpServletRequest request,
            HttpServletResponse response )
            throws IOException , ServletException{

            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
                out.println("Beer Selection Advice <br>");
            String c = request.getParameter("color");
                out.println("<br> Got beer color " + c);

        }
    }

Stacktrace in the terminal:

src/com/example/web/BeerSelect.java:3: error: package javax.servlet does not exist
import javax.servlet.*;
^
src/com/example/web/BeerSelect.java:4: error: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
src/com/example/web/BeerSelect.java:7: error: cannot find symbol
    public class BeerSelect  extends HttpServlet {
                                     ^
  symbol: class HttpServlet
src/com/example/web/BeerSelect.java:9: error: cannot find symbol
        public void doPost ( HttpServletRequest request,
                             ^
  symbol:   class HttpServletRequest
  location: class BeerSelect
src/com/example/web/BeerSelect.java:10: error: cannot find symbol
            HttpServletResponse response )
            ^
  symbol:   class HttpServletResponse
  location: class BeerSelect
src/com/example/web/BeerSelect.java:11: error: cannot find symbol
            throws IOException , ServletException{
                                 ^
  symbol:   class ServletException
  location: class BeerSelect
6 errors
  • Are you sure that q Servlet-api is on the classpath of your application? It seems not.

  • I don’t know.. how do I know? I set up Classpath in the terminal pointing to the Servlet jar

  • Post the commands you use in the terminal to compile.

1 answer

1


The error displayed is because the compiler cannot find and therefore cannot import javax.Servlet, because that jar is inside the web server. To compile properly via command line it is necessary to specify the path to the jar:

javac -classpath .:/caminho/para/o/jar/javax.servlet.jar com/example/web/BeerSelect.java

Source: https://stackoverflow.com/questions/5791446/compiling-servlets-with-javac

Browser other questions tagged

You are not signed in. Login or sign up in order to post.