0

I'm a newbie in Java EE. And I was following the steps of the book Head First Servlets and JSP to create a simple web application.

I wrote a simple servlet in Java. Now, I am planning to compile it and to put the class in the development environment. I'm not using any built tools, just with javac command in Windows. The servlet's path is D:\Learning Exercises\HeadFirstSnJ_Demo1\src\com\example\web\BeerSelect.java. And I plan to put the class file under the directory D:\Program Files (x86)\apache-tomcat-7.0.39\webapps\HeadFirstSnJ_Demo1\WEB_INF\classes\com\example\web\

The commands on the book are:

% cd MyProjects/beerV1
% javac -classpath /Users/bert/Applications2/tomcat/common/lib/servlet-api.jar:classes:. -d classes src/com/example/web/BeerSelect.java

I ran these command under Windows:

D:\>cd D:\Learning Exercises\HeadFirstSnJ_Demo1\src\com\example\web
D:\Learning Exercises\HeadFirstSnJ_Demo1\src\com\example\web>javac -classpath D:/"Program Files (x86)"/apache-tomcat-7.0.39/lib/servelt-api.jar;-d D:/"Program Files (x86)"/apache-tomcat-7.0.39/webapps/HeadFirstSnJ_Demo1/WEB_INF/classes/com/example/web BeerSelect.java

The system return an invalid flag error to me (I'm using a non-English OS. The following information is translated to English by myself):

javac: Invalid: D:/Program Files (x86)/apache-tomcat-7.0.39/webapps/HeadFirstSnJ_Demo1/WEB_INF/classes/com/example/web
Usage: javac <options> <source files>
-help to list all the valid options

Why did that happened? Am I using this javac -classpath -d command in a wrong way?

Actually, I think there is something wrong with the ";" here in front of "-d". If I replace the ";" with a space, there won't be this invalid flag error, BUT a "package javax.servlet doesn't exist" error instead, which is realy confused since I include the servlet-api.jar right here!

The corrected codes go here:

D:\Learning Exercises\HeadFirstSnJ_Demo1\src\com\example\web>javac -classpath "D:\Program Files (x86)\apache-tomcat-7.0.39\lib\servelt-api.jar" -d "D:\Program Files (x86)\apache-tomcat-7.0.39\webapps\HeadFirstSnJ_Demo1\WEB_INF\classes\com\example\web" BeerSelect.java
Angdi Chu
  • 73
  • 2
  • 12

3 Answers3

2

The spelling of "servlet-api.jar" is wrong.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Angdi Chu
  • 73
  • 2
  • 12
0

Use your directory navigation this way

"D:/Program Files (x86)/apache-tomcat-7.0.39/lib/servelt-api.jar"
sadhu
  • 1,429
  • 8
  • 14
  • Thx. I tried, but it didn't help. I still get the same error. – Angdi Chu Jun 03 '13 at 06:30
  • @Angdi This should help D:\Learning Exercises\HeadFirstSnJ_Demo1\src\com\example\web>javac -classpath "D:/Program Files (x86)/apache-tomcat-7.0.39/lib/servlet-api.jar" -d "D:/Program Files (x86)/apache-tomcat-7.0.39/webapps/HeadFirstSnJ_Demo1/WEB_INF/classes/com/example/web" BeerSelect.java – sadhu Jun 03 '13 at 06:43
  • I am thinking of the same thing. It did help for the invalid flag problem, BUT a "package javax.servlet doesn't exist" error appeared instead, which is realy confused since I've already include the servlet-api.jar right here! O_O – Angdi Chu Jun 03 '13 at 06:47
  • Not sure why its giving that error. This setup is working fine for me. Can you check if you have spelt the jar name correctly in your classpath? – sadhu Jun 03 '13 at 07:01
0

Use '\' for directory navigation in windows. [ '/' is for linux]. So try this:

D:\"Program Files (x86)"\apache-tomcat-7.0.39\lib\servelt-api.jar; -d D:\"Program Files (x86)"\apache-tomcat-7.0.39\webapps\HeadFirstSnJ_Demo1\WEB_INF\classes\com\example\web BeerSelect.java
kishoredbn
  • 2,007
  • 4
  • 28
  • 47
  • I don't think that is the problem...I tried but it's not helping. – Angdi Chu Jun 03 '13 at 06:33
  • one more try, take care of white-space: **javac -classpath** D:\"Program Files (x86)"\apache-tomcat-7.0.39\lib\servelt-api.jar; **-d** D:\"Program Files (x86)"\apache-tomcat-7.0.39\webapps\HeadFirstSnJ_Demo1\WEB_INF\classes\com\example\web BeerSelect.java – kishoredbn Jun 03 '13 at 06:37
  • Still not helping...Please see my updates to the question. Thx!! – Angdi Chu Jun 03 '13 at 06:42
  • but you using this: **d:/** !! Use this way **d:\ **. – kishoredbn Jun 03 '13 at 06:51
  • Well, I did tried. I corrected my command like this: javac -classpath "D:\Program Files (x86)\apache-tomcat-7.0.39\lib\servelt-api.jar" -d "D:\Program Files (x86)\apache-tomcat-7.0.39\webapps\HeadFirstSnJ_Demo1\WEB_INF\classes\com\example\web" BeerSelect.java And the system gave me a package javax.servlet doesn't exist" error instead – Angdi Chu Jun 03 '13 at 07:00
  • [Check this](http://stackoverflow.com/questions/9193228/compile-error-package-javax-servlet-does-not-exist) . Hope this helps. – kishoredbn Jun 03 '13 at 07:17