Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

error: package javax.servlet.http doesn't exist  RSS feed

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want compile and run servlet whose response object prints html tags which contains welcome message to user in form of html page.
The code of WelcomeServletMesssage.java
Placed location:C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\WEB-INF\classes\WelcomeServletMesssage.java

Googled error, found I should create new user variable to set path for servlet-api.jar so then it can be used.
In environment variables--> user variables
I created new user variable, and set Variable name: CLASSPATH and Variable value: C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\ where my servlet-api.jar resides but still it gives same error. can somebody help me how to figure out this error..
 
Aditya Mukundan
Greenhorn
Posts: 17
Eclipse IDE Linux Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what IDE you use?did you add tomcat directory to the project?
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pardon me for delaying, didn't have net whole day. I solved the issue I had to copy servlet-api.jar in C:\Program Files\Java\jdk1.8.0_40\jre\lib\ext\. I'm using command prompt not any IDE
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a bad way to solve the problem. You should leave the servlet jar where it is and make sure it is part of the class path.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I deleted that copied servlet-api.jar from C:\Program Files\Java\jdk1.8.0_40\jre\lib\ext\ where I'd placed manually. while compiling servlet it gives error import javax.servlet.http doesn't exist. what should I do then please need help ....
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same way I had to copy mysql-connector-java-5.1.19 jar file at location C:\Program Files\Java\jdk1.8.0_40\jre\lib\ext\ while doing database programs then it worked. I think here I'm doing it manually by copying jars in appropriate folder and when using IDE like NetBeans we also add jar files like mysql-connector etc. in library folder in project, correct me if I'm wrong...so what's wrong if I put that servlet-api.jar file in that jdk folder?
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you go copying library jars into your projects, you are probably wasting disk space, and potentially creating defective webapps. If you copy library jars into the lib directory of your JDK, they may not work because the JDK security won't recognized their contents as being authorized for that location.

To compile a program using a JDBC jar or servlet-api jar from the command line, add the jar file path(s) to the classpath on the javac command line. If you are using an IDE, you can usually set an equivalent in the project configuration dialog. In Eclipse, for example, I can assign a symbolic name to those jars and tell my project to use that name to locate the jars (and their contained classes) when the IDE compiles my source code.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes It worked! Though I deleted servlet-api.jar file from C:\Program Files\Java\jdk1.8.0_40\jre\lib\ext\ where I'd placed manually. I actually earlier didn’t mention servlet-api.jar file name in environment CLASSPATH variables.
Variable-Name: CLASSPATH
Wrong one:
Variable-value: C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib
Correct one which worked:
Variable-value: C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar
As Be careful relying on the CLASSPATH too much.
I thought to use command line then
My servlet is WelcomeServletMesssage.java is at location C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\FirstServletApp\WEB-INF\classes\WelcomeServletMesssage.java

I compiled it successfully in these 3 ways:
  • Go to the folder where WelcomeServletMesssage.java is placed i.e. C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\FirstServletApp\WEB-INF\classes\
    Then set CLASSPATH = C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar
    Then javac myservlet.java


  • Javac –classpath “servlet-api.jar with its location” “my WelcomeServletMesssage.java with its location”
    Whole command becomes like this:
    Javac –classpath “C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar” “C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\FirstServletApp\WEB-INF\classes\ WelcomeServletMesssage.java”


  • Go to location where WelcomeServletMesssage.java is placed ( I used batch file to go to this location directly ) i.e.C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\FirstServletApp\WEB-INF\classes\ then set classpath
    Whole command becomes like this:
    C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\FirstServletApp\WEB-INF\classes\ javac –classpath C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar WelcomeServletMesssage.java


  • Correct me If I'm wrong using command line to compile my WelcomeServletMesssage.java..
    Thank you so much Tim, Bear and Aditya for replying...
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm using windows batch file to go to the directory location C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps where I placed my all applications folders so I don't have to type in command line every time when I want to compile them.
    Here is code of my webapppath.bat file:

    when I double click this webapppath.bat file or run in command line like c:\>webapppath.bat, I go to this directory path as I wanted to go
    c:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps> ( Now partially I'm already in this directory because of webapppath.bat file now I go to next directories i.e.FirstServletApp\WEB-INF\classes\ by manually typing in command line) BUT when I use third type ( as above mentioned ) to compile WelcomeServletMesssage.java which is
    C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\FirstServletApp\WEB-INF\classes\ javac –classpath C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar WelcomeServletMesssage.java
    It doesn't compile WelcomeServletMesssage.java and gives error:
    javac: invalid flag: Files\apache
    Usage: Javac <options> <source files>
    use -help for possible options

    BUT when I type same path manually in command line which is:
    C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\FirstServletApp\WEB-INF\classes\ javac –classpath C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar WelcomeServletMesssage.java
    It compiles successfully.
    It gives above error only when I use webapppath.bact file to go directly to directory C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps can you please guide me whether I created .bat file is valid or not...
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!