• Post Reply Bookmark Topic Watch Topic
  • New Topic

javax.servlet not found  RSS feed

 
Eric Racin
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I installed Tomcat7 and JDK8. When I run batch file in cmd window:
"C:\Program Files\Java\jdk1.8.0_40\bin\javac" -cp C:\Tomcat7\lib\servlet-api.jar C:\G\j_javac\ServletCarbon4.java

I get the error msg: error: package javax.servlet does not exist
Here is the beginning of the java code:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think servlets are part of the standard Java® installation. Have you tried installing JavaEE?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JEE is not needed when just using servlets with Tomcat. It looks like he's trying to include the servlet jar from Tomcat, which is all that is needed. Perhaps the command line is wrong (not a Windows user, so not sure).
 
Eric Racin
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thougtht that the command: -cp C:\Tomcat7\lib\servlet-api.jar
would include the class files for Servlets, including the import statement: javax.servlet
 
Eric Racin
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is part of a 3-Tier Client/Server system that runs fine: I am just updating the class files for Tomcat 7 (was Tomcat 4).
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have thought that the servlet classes would get included in the class file, too. Unless you got the location of the jar wrong. The compiler won't tell you if you include a non-existent jar in the classpath, it will just ignore it. So are you sure that's the correct path to the jar on your computer?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the class you are compiling in a package?
The file you are compiling: "C:\G\j_javac\ServletCarbon4.java" seems to indicate that you aren't, and the "beginning of the java code" doesn't include it.

They made some changes to the java compiler such that classes in the default package can no longer import anything that IS in a package.
Its a security fix that came in years ago now.
However if this was originally Tomcat4 code, then that IS long enough ago that the issue could be biting you.

Put the ServletCarbon4 class into a package, and try recompiling it.
 
Eric Racin
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never explicitly used packages, so I would like to avoid their use for now if possible.
I placed the servlet-api.jar file in the same directory that I am compiling, so the batch file looks like:
"C:\Program Files\Java\jdk1.8.0_40\bin\javac" -cp servlet-api.jar C:\G\j_javac\ServletCarbon4.java
I still get the error msg: error: package javax.servlet does not exist
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote: . . . classes in the default package can no longer import anything that IS in a package. . . .
Are you sure about that? I thought it was the other way round: classes in a package cannot import anything from the unnamed package. I can't seem to find that requirement in the Java8 JLS; has it been moved?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never explicitly used packages, so I would like to avoid their use for now if possible.


Better revise that if you are going to be working with servlets. Not using packages leads to all sorts of mysterious errors as seen in many posts in the servlet forum.

Bill
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never explicitly used packages, so I would like to avoid their use for now if possible.


Using packages is a requirement when programming servlets and is a skill you should master before diving off into servlets.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Racin wrote:I placed the servlet-api.jar file in the same directory that I am compiling

Moving and copying the servlet jar file around is a sure way to create lots of obscure and gnarly problems for you down the road. Return the jar file to it original Tomcat location if you moved it. If you copied, remove all copies and use the jar file in place form the Tomcat folder.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Stefan Evans wrote: . . . classes in the default package can no longer import anything that IS in a package. . . .
Are you sure about that? I thought it was the other way round: classes in a package cannot import anything from the unnamed package. I can't seem to find that requirement in the Java8 JLS; has it been moved?


Oops. Good catch. It IS the other way around.
But I think the package requirement still exists if you want to run a servlet. Maybe it is that the servlet container can't load classes from the default package? Its been so long since I tried NOT using packages I've forgotten the details.


 
Eric Racin
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bottom line is: The Servlet in question has worked flawlessly for 5 years. I have the motto: if its not broke, don't fix it.
All I want to do now is recompile a robust Servlet with the Tomcat 7 libraries instead of Tomcat 4. I will remove the servlet-api.jar from the directory (I didn't realize that was a bad idea).
But I am still left in a quandary about why my Path statements don't seem to be bringing in the appropriate import javax.servlet.* to compile against.
 
Eric Racin
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They made some changes to the java compiler such that classes in the default package can no longer import anything that IS in a package.
Its a security fix that came in years ago now.

But I guess from the statement above, I will have to dive into Packages to get this working.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!