Eclipse Quick Tip: Resolving Error “The import javax.servlet cannot be resolved”

When you start to develop web application using Java, you may think to use Eclipse as the IDE. Assume that you have seen a good tutorial about Java servlet on the internet or read the chapter of a book discussing about web development using JSP.

You are now writing the first servlet by using the wizard provided by the IDE. However, as soon the template file is loaded on the source code editor, you encounter the infamous error “The import javax.servlet cannot be resolved” just like depicted in the figure below.

Servlet import error


This error is normal. Servlet library is not shipped by default in Eclipse. You also can’t find the library in JRE/JDK package you download. Instead, you will find it in the lib folder under Tomcat installation directory like depicted below.

Servlet API jar file

So, how can we resolve the error? The solution is apparently simple. We just need to add the servlet jar file into the build path. Based on the scope, we can achieve this using two methods; single project-wise and all projects-wise. Both methods will be explained immediately. You just need to pay attention that these methods were tested under the following system environment and steps in other environment may vary:

OS: Windows
Eclipse version: Eclipse 3.5 Ganymede
JDK version: JDK 1.6.0_14

In the first method, we add the servlet-api jar by right-clicking the project name and choose Properties option. After clicking, a new popup window will appear. Click the “Java Build Path” menu on the left followed by choosing “Libraries” tab. Subsequently, click “Add External JARs” button and point to servlet-api.jar in lib folder of Apache Tomcat installation directory. If you do all these steps correctly, you will notice that servlet-api.jar has been added into the libraries. Please check your result with the reference picture below.

Adding servlet API into project library

Click “OK” to complete the modification. After a glance, you will notice all imports errors have been disappeared.

The first method is good if you rarely work with servlet. If you yourself are a very busy developer who has to build numerous projects for your vast client base, it is considerably wise to apply the second method. In this method, we will add the servlet-api.jar into the JDK directory. After the modification, you will never encounter the error again as the servlet-api.jar will always be in the build path. This is different with the previous method which requires you to modify the project properties again if you create a new dynamic web project.

To execute the second method, follow the instructions below:
1. In Eclipse workbench, choose Window > Preferences
2. Choose Java > Installed JREs on the left menu
3. Focus on the JDK you want to modify, click “Edit”. A new popup will appear

Adding servlet API JAR file into universal build path

4. Click “Add External JARs” and point to servlet-api.jar in lib folder of Tomcat installation directory.

Adding Servlet API JAR file into universal build path-2

5. Click “Finish” to close the second popup and “OK” to complete the modification

After you do all the steps, all the servlet import errors in the source code editor will have vanished. You will also have discarded the import error for all projects which require the servlet functionalities.

97 thoughts on “Eclipse Quick Tip: Resolving Error “The import javax.servlet cannot be resolved”

  1. Jass

    The first one works fine for me, but the second gives several Java Problems message like

    “Access restriction: The type HttpServletResponse is not accessible due to restriction on required library C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar”

    I don’t know these are errors or warnings, anyway my program can still run as it should be.

    My system:

    “JRE 1.6 update 22”

    “Eclipse Java EE IDE for Web Developers.
    Version: Helios Service Release 1
    Build id: 20100917-0705”

    Thanks! I will go on.

    Reply
  2. Tech Admin Post author

    @Jass
    make sure it was a copy-and-paste process instead of a cut-and-paste. also ensure that tomcat instance is not running when you invoke the operation.

    Reply
  3. Anthony Myers

    Thank you SO much for publishing the solution to this problem! I just put eclipse on a new computer and couldn’t figure out the problem.

    Reply
  4. Ali

    Thanks for your explain.
    Also If you have added tomcat server to eclipse already, you can click on add library and select tomcat from Server Run-time section. and you don’t need to import it from external jar file yourself.

    Reply
  5. rosh

    Thanks !
    All errors disappeared except the one for
    “import javax.servlet.annotation.WebServlet;”

    I’m using Eclipse 3.7.0 on Mac OSX 10.6.7 and imported lib from Tomcat 6

    Reply
  6. olli

    Hey rosh,

    simply add below properties of the project a new runtime containing tomcat 7 as target platform and select it. After refreshing the project and moving to libraries you will see the jar and you can compile your webapp including annotations like @WebServlet.

    Reply
  7. Tech Admin Post author

    @rosh
    these days i prefer using maven to manage the building configuration. using this approach, such kind of library dependencies will be easier to be solved. i will come with posts about maven later.

    Reply
  8. Krishna

    Thank Dear,
    I got the result.I was trying to copy the servlet-api.jar file to lib directory of WEB-INF.
    Thanks once again.

    Reply
  9. ArrGee

    โ€œAccess restriction: The type HttpServletResponse is not accessible due to restriction on required library C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jarโ€

    On the Java Build Path Libraires tab open your .jar to see the Access rules.
    Edit…
    Add…
    Resolution: Accessible
    Rule Pattern:*

    Reply
  10. shirish herwade

    I’m still having the problem. I’m trying to use these classes:
    import javax.ws.rs.Consumes;
    import javax.ws.rs.FormParam;
    import javax.ws.rs.GET;
    import javax.ws.rs.POST;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.Path;
    but having the error “The import jawax.ws can not be resolved”

    Reply
  11. shirish herwade

    I’m still having the problem. I’m trying to use these classes:
    import javax.ws.rs.Consumes;
    import javax.ws.rs.FormParam;
    import javax.ws.rs.GET;
    import javax.ws.rs.POST;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.Path;
    but having the error “The import jawax.ws can not be resolved”.
    Pleaze help me. Thanks in advance.

    Reply
  12. Tech Admin Post author

    hi shirish,
    javax.ws.rs is not a part of java servlet specifications. it’s the api implementation of JSR-311 (Java API for RESTful web service). You can search the net with keyword “jsr311-api jar” or use dependency management like maven to handle this dependency. i will come with some articles about maven soon.

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.