0

I am trying to implement the basic HelloServlet.java program. But it seems to be producing these errors

HelloServlet.java:7: error: cannot find symbol
public class HelloServlet extends HttpServlet {
                                  ^
  symbol: class HttpServlet
HelloServlet.java:11: error: cannot find symbol
   public void init() throws ServletException {
                             ^
  symbol:   class ServletException
  location: class HelloServlet
HelloServlet.java:16: error: cannot find symbol
   public void doGet(HttpServletRequest request, HttpServletResponse response)
                     ^
  symbol:   class HttpServletRequest
  location: class HelloServlet
HelloServlet.java:16: error: cannot find symbol
   public void doGet(HttpServletRequest request, HttpServletResponse response)
                                                 ^
  symbol:   class HttpServletResponse
  location: class HelloServlet
HelloServlet.java:17: error: cannot find symbol
      throws ServletException, IOException {
             ^
  symbol:   class ServletException
  location: class HelloServlet
HelloServlet.java:3: error: package javax.servlet does not exist
import javax.servlet.*;
^
HelloServlet.java:4: error: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
7 errors

I have added the CLASSPATH fot the jar files

nithinchandranp@b-40:~$ echo ${CLASSPATH}
/home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/*
nithinchandranp@b-40:~$ 

Now what am I doing wrong? Please help..

Nithin cp
  • 109
  • 2
  • 3
  • 11

3 Answers3

1

When you compile your java class you can use the -cp arguments to provide the classpath / libraries required by your class. For your case you'll need to provide to servlet-api.jar. You can compile your Servlet like:

javac -cp /usr/local/Cellar/tomcat/9.0.6/libexec/lib/servlet-api.jar HelloServlet.java

W-S
  • 516
  • 5
  • 14
  • When i checked other questions on Stackoverflow, i did find this way of compiling the java files. It just worked fine for me. But using the CLASSPATH is a lot simpler right? like we dont have to mention all the jar files during the compilation time right. – Nithin cp Oct 04 '18 at 11:05
1

It was a mistake on my part.

This was how i created my CLASSPATH through the command line

 export CLASSPATH=/home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/*

The directory I created was not ApacheTomcat, but it was Apache Tomcat(with a space in between).

Maybe that is why the CLASSPATH variable couldn't recognize the correct location.

I changed the directory name into ApacheTomcat(removed the space).

Now i defined the CLASSPATH variable in the .bashrc file as follows:

#classpath
export CLASSPATH=/home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/*

Now when i do echo${CLASSPATH}, I get all the jar files which i mentioned.

nithinchandranp@b-40:~$ echo ${CLASSPATH}
/home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/annotations-api.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/catalina-ant.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/catalina-ha.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/catalina.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/catalina-storeconfig.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/catalina-tribes.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/ecj-4.6.3.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/el-api.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/jasper-el.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/jasper.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/jaspic-api.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/jsp-api.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/servlet-api.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-api.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-coyote.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-dbcp.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-i18n-es.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-i18n-fr.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-i18n-ja.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-i18n-ru.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-jdbc.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-jni.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-util.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-util-scan.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-websocket.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/websocket-api.jar
Nithin cp
  • 109
  • 2
  • 3
  • 11
1

A Classpath with * at the End does not work with jar Datasets. You have to put each jar in your classpath.

MyOggRadio
  • 34
  • 10
  • Like I mentioned above in the answer, I had used the wildcard(*) to include all the jar files in a particular directory and all the jar files did get added to the CLASSPATH. – Nithin cp Oct 05 '18 at 17:19
  • Adding it individually is not necessary – Nithin cp Oct 05 '18 at 17:19