Search code examples
javatomcatjakarta-eetomcat10

How do I install Java EE SDK?


I'm covering a chapter for university where I'm getting into servlets. The book requires me to copy a piece of code and run it in cmd. It won't run in cmd and the chapter vaguely explains how I can install Java EE. Here is the code and the error:

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

public class NameServlet extends HttpServlet {
    
    public static void main(String[] args) {

    }

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

        out.println("<html>");
        out.println("<body style=’background-color: lightyellow’>");

        String name = "Wow, this servlet works great!!";
        out.println("Name: " + name);
        out.println("</body>");
        out.println("</html>");
        out.close();
    }

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

Error:


Solution

  • The problem is that you're using Apache Tomcat 10, which uses the latest version of Jakarta EE (version 9, the successor of Java EE 8). The big change in Jakarta EE 9 is that it switched the namespace of its packages from javax.* to jakarta.*.

    You either need to:

    • Use Apache Tomcat 9 (or earlier, or another Java EE servlet container that still uses the Java EE 8 or earlier and the javax.* namespace), or
    • Switch to using the new Jakarta EE namespace (in your case, packages jakarta.servlet and jakarta.servlet.http).

    As mentioned on the Tomcat 10 download page:

    Users of Tomcat 10 onwards should be aware that, as a result of the move from Java EE to Jakarta EE as part of the transfer of Java EE to the Eclipse Foundation, the primary package for all implemented APIs has changed from javax.* to jakarta.*. This will almost certainly require code changes to enable applications to migrate from Tomcat 9 and earlier to Tomcat 10 and later.