错误:即使添加了CLASSPATH变量,程序包javax.servlet.http也不存在

时间:2018-10-04 07:03:36

标签: java tomcat servlets path classpath

我正在尝试实现基本的HelloServlet.java程序。 但似乎正在产生这些错误

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

我在jar文件中添加了CLASSPATH

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

现在我在做什么错? 请帮忙。

3 个答案:

答案 0 :(得分:0)

编译Java类时,可以使用-cp参数提供类所需的类路径/库。对于您的情况,您需要提供给servlet-api.jar。您可以像这样编译Servlet:

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

答案 1 :(得分:0)

这是我的错误。

这就是我通过命令行创建CLASSPATH的方式

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

我创建的目录不是ApacheTomcat,而是Apache Tomcat(之间有一个空格)。

也许这就是为什么CLASSPATH变量无法识别正确位置的原因。

我将目录名称更改为ApacheTomcat(删除了空格)。

现在,我在.bashrc文件中定义了CLASSPATH变量,如下所示:

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

现在,当我执行echo $ {CLASSPATH}时,我会得到我提到的所有jar文件。

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

答案 2 :(得分:0)

在末尾带有*的类路径不适用于jar数据集。您必须将每个jar放在类路径中。