No Image

Package javax servlet does not exist

СОДЕРЖАНИЕ
11 просмотров
10 марта 2020

I have a package in which I import javax.servlet.* and javax.servlet.http.* When I try to compile it in command prompt I get the error

package javax.servlet does not exist

I use JDK 1.7.0 and Tomcat 6.0.

11 Answers 11

You need to add the path to Tomcat’s /lib/servlet-api.jar file to the compile time classpath.

The classpath is where Java needs to look for imported dependencies. It will otherwise default to the current folder which is included as . in the above example. The ; is the path separator for Windows; if you’re using an Unix based OS, then you need to use : instead.

I have the jre7 and jdk1.7.0 installed along with the Tomcat 7.0 but it shows this error. servlet.http is not the only one that "does not exist", there are also other (servlet.) components.

Can anybody help me with this?

UPDATE: This error occurs when I try the follow command: javac -classpath servlet-api.jar WebTest.java

11 Answers 11

Your CLASSPATH variable does not point to the directory containing the javax classes. The CLASSPATH variable specifies where the java compiler should look for java class file resources. If it does not know to look in the javax directory, then it will never find the file(s) you are after.

If you are working with maven project, then add following dependency to your pom.xml

If you’re using the command console to compile the servlet, then you should include Tomcat’s /lib/servlet-api.jar in the compile classpath.

(use ; instead of : as path separator in Windows)

If you’re using an IDE, then you should integrate Tomcat in the IDE and reference it as target runtime in the project. If you’re using Eclipse as IDE, see also this for more detail: How do I import the javax.servlet API in my Eclipse project?

Or on Windows, try:

If you are using Ant and trying to build then you need to :

Читайте также:  Как написать вертикальную черту на клавиатуре

Specify tomcat location by

Add tomcat libs to your already defined path for jars by

This error occurs when you compile a java program using classes that support the Servlet API. The compiler searches for the library (included in a .jar file) by using the CLASSPATH. You can specify this when you compile using -classpath or -cp options as noted in other responses, but you should set up your environment to define the classpath as needed.

Set the CLASSPATH environment variable to reference the location of servlet-api.jar , which depends on your setup (OS, how you installed, etc.)

Assuming you’re using Tomcat and have installed it in one of 20 possible ways, the APIs used by servlets will be installed on your system, relative to wherever Tomcat is installed. For historical reasons, Tomcat is also known as "Catalina", so you can use the command "catalina" to run certain commands, and alone, it will report, amongst other things the CATALINA_BASE . For example on my Mac using Tomcat installed using homebrew it’s

The location of the Tomcat servlet libraries is under this in the lib directory. Set CATALINA_BASE, then set CLASSPATH using the base as a start, for example for Linux or OSX you might set this in .profile , or .bash_profile like so:

Exit the terminal/shell and come back in to run the profile. You should be able to see that the variable is set by using the echo command, e.g.

If it displays the full path to the jar `javac WebTest.java’ compile your class.

Other answers are correct — set up your IDE (Eclipse, IntelliJ) to know about Tomcat or build with Maven and you’ll save pain.

Dear All,
I am a newbie to JAVA.
I installed JDK1.6.0_04, then NetBeans IDE 6.0
I tried compiling an example servlet, but got this error message:
package javax.servlet does not exist, package javax.servlet.http does not exist

Читайте также:  Hp deskjet d1400 series драйвер

I tried locating these packages, but in vain. I also tried setting the CLASSPATH to many values, still it did not work.
Can anyone, please, help me with this.
Should I be able to see/locate these packages, do I have to download them?

Thank you very much.

  • 9341 Просмотров
  • Метки: нет (добавить)
1. Re: package javax.servlet does not exist, package javax.servlet.http does n

  • Мне нравится Показать отметки "Мне нравится" (0) (0)
  • Действия
2. Re: package javax.servlet does not exist, package javax.servlet.http does n

davidgoodman wrote:
Dear All,
I am a newbie to JAVA.
I installed JDK1.6.0_04, then NetBeans IDE 6.0
I tried compiling an example servlet, but got this error message:
package javax.servlet does not exist, package javax.servlet.http does not exist

What servlet/JSP engine do you plan to deploy to? Tomcat, perhaps?

If so, you’ll need to put the servlet JAR in your CLASSPATH. (servlet-api.jar for Tomcat 5.5 and earlier, servlet.jar for 6.0 and later.)

I tried locating these packages, but in vain.

I also tried setting the CLASSPATH to many values, still it did not work.

Can anyone, please, help me with this.
Should I be able to see/locate these packages, do I have to download them?

You have to get a servlet/JSP engine. Step 1.

  • Мне нравится Показать отметки "Мне нравится" (0) (0)
  • Действия
3. Re: package javax.servlet does not exist, package javax.servlet.http does n

Thank you for your help.
Here is the code:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ExampServlet extends HttpServlet <

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
<
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(" Example " +
" ");

Button Clicked

String DATA = request.getParameter("DATA");

if(DATA != null) <
out.println(DATA);
> else <
out.println("No text entered.");
>

  • Мне нравится Показать отметки "Мне нравится" (0) (0)
  • Действия
Читайте также:  Как почистить телефон сони xperia
4. Re: package javax.servlet does not exist, package javax.servlet.http does n

davidgoodman wrote:
Dear All,
I am a newbie to JAVA.
I installed JDK1.6.0_04, then NetBeans IDE 6.0
I tried compiling an example servlet, but got this error message:
package javax.servlet does not exist, package javax.servlet.http does not exist

What servlet/JSP engine do you plan to deploy to? Tomcat, perhaps?

I think it’s Tomcat (C:Program FilesApache Software FoundationApache Tomcat 6.0.14). Are there other engines that come with NetBeans IDE 6.0 (when I downloaded NetBeans, I chose ALL)

If so, you’ll need to put the servlet JAR in your CLASSPATH. (servlet-api.jar for Tomcat 5.5 and earlier, servlet.jar for 6.0 >and later.)

I tried locating these packages, but in vain.

You won’t find them until you realize that you have to deploy that servlet someplace.

I also tried setting the CLASSPATH to many values, still it did not work.

See above. What were you planning to set CLASSPATH to?

Can anyone, please, help me with this.
Should I be able to see/locate these packages, do I have to download them?

You have to get a servlet/JSP engine. Step 1.

  • Мне нравится Показать отметки "Мне нравится" (0) (0)
  • Действия
5. Re: package javax.servlet does not exist, package javax.servlet.http does n

Setting your classpath to the lib directory is not enough. The classes you need are packaged in a .jar file, so you need to add the jar file itself explicitly to your classpath. A directory is only enough when dealing with .class files.

  • Мне нравится Показать отметки "Мне нравится" (0) (0)
  • Действия
6. Re: package javax.servlet does not exist, package javax.servlet.http does n

Thank you very much for your help.
As you kindly suggested, I set the CLASSPATH environment variable to:
C:Program FilesApache Software FoundationApache Tomcat 6.0.14libservlet-api.jar

Thank you very much again and have a good day.

Комментировать
11 просмотров
Комментариев нет, будьте первым кто его оставит

Это интересно
No Image Компьютеры
0 комментариев
No Image Компьютеры
0 комментариев
No Image Компьютеры
0 комментариев
No Image Компьютеры
0 комментариев
Adblock detector