2

I am following Head First: Servlet and JSP and I was writing my first Servlet.
I tried to compile it using command line and I got an error that package javax.servlet does not exist.

So far, I have set the JAVA_HOME variable to the value: C:\Program Files\Java\jdk1.7.0_11 and the PATH variable also to C:\Program Files\Java\jdk1.7.0_11\bin

How do I get the javax.servlet package?

Also, I have installed JDK 7 update 11 after the applet bug that was being exploited.
I have both JDK Update 11 and JDK Update 9 installed.
Should I get rid of Update 9 or is it not needed ?

Misc

Operating System: Windows 7 Home Premium 32 bit

What I also tried:

javac -cp .;D:\Program Files\apache-tomcat-7.0.35-windows-x86\apache-tomcat-7.0.35\lib\servlet-api.jar Ch1servlet.java

I tried both forward slash AND backward slash. In both the cases I got an invalid flag error.

An SO User
  • 24,612
  • 35
  • 133
  • 221

2 Answers2

4
javac -cp .;D:\Program Files\apache-tomcat-7.0.35-windows-x86\apache-tomcat-7.0.35\lib\servlet-api.jar Ch1servlet.java

You're 99% of the way there, you just need to put some extra quotes in because the path to your servlet API jar contains spaces. Try this:

javac -cp ".;D:\Program Files\apache-tomcat-7.0.35-windows-x86\apache-tomcat-7.0.35\lib\servlet-api.jar" Ch1servlet.java
Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • Worked!! Can I set any environment variable to do this automatically ? So that I will not have to type the verbose path again and again? :) – An SO User Jan 30 '13 at 11:46
  • 2
    `set $CLASSPATH=%CLASSAPTH%;D:\Program Files\apache-tomcat-7.0.35-windows-x86\apache-tomcat-7.0.35\lib\servlet-api.jar;` – TheWhiteRabbit Jan 30 '13 at 11:50
  • 1
    @LittleChild I wouldn't recommend using the command line `javac` directly for anything but the simplest one off compiles. At the very least you should write a `.bat` file or similar to call the compiler with the right options, but better would be to learn how to use a proper build tool such as Ant and/or an IDE such as Eclipse to handle the complexity for you. – Ian Roberts Jan 30 '13 at 11:50
  • @IanRoberts I will heed BalusC's advise and learn the servlets hard way first. This was just the beginning. I have 2000 pages ahead of me! I hope they will explain later what you are saying. **What about the JDK thing?** – An SO User Jan 30 '13 at 11:55
  • 1
    @LittleChild as a professional developer of client side software I find it handy to keep a few different versions of the JDK so that when I receive a bug report I can reproduce it on the same version that the client is using. But if you're just doing server-side development and you control the server's JDK version then that's not so necessary. – Ian Roberts Jan 30 '13 at 12:15
2

It will not be because

  • Servlets are part of Java-EE and not Java-SE
  • You need to download and add java-ee libraries if you need to use servlets
TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57