How to compile a servlet for Tomcat in command line? error: package javax.servlet does not exist

前端 未结 4 1343
孤独总比滥情好
孤独总比滥情好 2021-01-03 09:37

I\'ve got this error message when I compiled a Java file :

error: package javax.servlet does not exist

I installed a big .SH file for Jave

4条回答
  •  我在风中等你
    2021-01-03 10:17

    You need to include the servlet-api JAR in the compile time classpath.

    If you are using maven add this as a dependency in the pom.xml.

    
        javax.servlet
        servlet-api
        2.5
        provided
    
    

    That wil include the dependency at compile time and use the Tomcat one at runtime.

    If not you should add Tomcat as project target runtime through Eclipse.

    This questions has some useful info on including these in an Eclipse project: How do I import the javax.servlet API in my Eclipse project?

    If you are using command line to build the project, you will most likely need to add these to the classpath argument to javac to add these jars to the classpath.

    See this question: How to compile servlets from command prompt?

    The key part is:

    javac -classpath C:\apache-tomcat-7.0.23\lib\servlet-api.jar MyTestServlet.java
    

提交回复
热议问题