Issue
I have set class path as follow: CLASS_PATH: C:\Program Files\Apache Software Foundation\Tomcat 10.0\lib;C:\Program Files (x86)\Java\mysql-connector.jar;C:\Program Files (x86)\Java\sqljdbc_8.2\enu\mssql-jdbc-8.2.0.jre8.jar;
and I also tried using jar file at compile time:- java -cp C:\Program Files\Apache Software Foundation\Tomcat 10.0\lib\servlet-api.jar Hello.java
java -cp "C:\Program Files\Apache Software Foundation\Tomcat 10.0\lib\servlet-api.jar" Hello.java
java -cp .;C:\Program Files\Apache Software Foundation\Tomcat 10.0\lib\servlet-api.jar Hello.java
java -cp ".;"C:\Program Files\Apache Software Foundation\Tomcat 10.0\lib\servlet-api.jar" Hello.java
java -cp C:\Program Files\Apache Software Foundation\Tomcat 10.0\lib\servlet-api.jar;. Hello.java
java -cp "C:\Program Files\Apache Software Foundation\Tomcat 10.0\lib\servlet-api.jar";. Hello.java
I also tried replacing -cp with classpath but still I get the following errors every single time:
C:\Program Files\Apache Software Foundation\Tomcat 10.0\webapps\16 servlet\WEB-INF\classes>javac -cp .;servlet-api.jar; Hello.java
Hello.java:2: error: package javax.servlet does not exist
import javax.servlet.*;
^
Hello.java:3: error: cannot find symbol
public class Hello implements Servlet
^
symbol: class Servlet
Hello.java:5: error: cannot find symbol
public void init(ServletConfig sc)throws ServletException
^
symbol: class ServletConfig
location: class Hello
Hello.java:5: error: cannot find symbol
public void init(ServletConfig sc)throws ServletException
^
symbol: class ServletException
location: class Hello
Hello.java:9: error: cannot find symbol
public ServletConfig getServletConfig()
^
symbol: class ServletConfig
location: class Hello
Hello.java:13: error: cannot find symbol
public void service(ServletRequest req,ServletResponse res)throws ServletException
^
symbol: class ServletRequest
location: class Hello
Hello.java:13: error: cannot find symbol
public void service(ServletRequest req,ServletResponse res)throws ServletException
^
symbol: class ServletResponse
location: class Hello
Hello.java:13: error: cannot find symbol
public void service(ServletRequest req,ServletResponse res)throws ServletException
^
symbol: class ServletException
location: class Hello
8 errors
My Hello.java Servlet file:-
import java.io.*;
import javax.servlet.*;
public class Hello implements Servlet
{
public void init(ServletConfig sc)throws ServletException
{
}
public ServletConfig getServletConfig()
{
return null;
}
public void service(ServletRequest req,ServletResponse res)throws ServletException
{
String name=req.getParameter("name");
PrintWriter out=res.getWriter();
out.println("Hello "+name);
}
public String getServletInfo()
{
return null;
}
public void destroy()
{
}
}
tbh I tried other Servlet java program as well on netbeans IDE and it is running perfectly but I am not able to configure it explicitly myself even after searching and trying every possible solution out there, guys please help me resolve it.
Solution
Tomcat 10 is currently a milestone build, so not really good for first steps or meaningful development work, unless you specifically want to go after the root cause of your problem:
Tomcat 10 is not backwards compatible as the whole Java EE (or whatever label it flew under) is now replaced with the new Jakarta EE, thus your import javax.servlet.*
now needs to be import jakarta.servlet.*
.
Instead of adapting everything right now, you'll probably be happier by just utilizing Tomcat 9 and go with the old and (documented everywhere) javax.servlet.*
Answered By - Olaf Kock
Answer Checked By - Katrina (JavaFixing Volunteer)