Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head First JSP and Servlets, page 85. COmpiling the Servlet version 2.

 
Carlos L Sanchez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well i have made it this far in this book. Here the author asks the reader to compile once more the new modified servlet.
There is a dependency now, since the new servlet has an import of com.example.model in order to use the new class BeerExpert in the servlet.
I created said class and compiled it and it saved the .class file in the reight place accorgind to the model ex: classes\com\example\model\BeerExpert.class
here is my command line imput:
C:\Users\Carlos L\Tomcat\My Tomcat Projects\beer-v1>javac -classpath "C:\Users\Carlos L\Tomcat\apache-tomcat-8.0.28\lib\servlet-api.jar";classes:. -d classes src\com\example\web\BeerSelect.java

the path to the BeerExpert.java file is:
C:\Users\Carlos L\Tomcat\My Tomcat Projects\beer-v1\src\com\example\model


and these are the error messages i get after compiling the file:
package com.example.model.* does not exist
package java.servlet.* does not exist
package java.servlet.http* does not exist
and many others
Can someone tell my what is the problem here?
 
Stefan Evans
Bartender
Posts: 1784
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#1: You need to compile from the "root" directory of your package hierarchy, or specify a source path. I think in this case this means the "src" directory.
#2: I think the quotes need to go around the ENTIRE argument for one parameter (I might be wrong)
#3: You should be using a semicolon (;) to separate classpath elements on windows. You have a colon ( separating classes and the dot. Use a semicolon on windows, and a colon on unix.

Apologies, I haven't actually tried this out (and it has been ages since I used the command line) but try variations around those suggestions.

So
#1 - change directory to C:\Users\Carlos L\Tomcat\My Tomcat Projects\beer-v1\src
#2 - javac -classpath "C:\Users\Carlos L\Tomcat\apache-tomcat-8.0.28\lib\servlet-api.jar;..\classes;." -d../classes com\example\web\BeerSelect.java

or maybe

javac -classpath "C:\Users\Carlos L\Tomcat\apache-tomcat-8.0.28\lib\servlet-api.jar;classes;." -d classes -sourcepath src com\example\web\BeerSelect.java


 
Carlos L Sanchez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:#1: You need to compile from the "root" directory of your package hierarchy, or specify a source path. I think in this case this means the "src" directory.
#2: I think the quotes need to go around the ENTIRE argument for one parameter (I might be wrong)
#3: You should be using a semicolon (;) to separate classpath elements on windows. You have a colon ( separating classes and the dot. Use a semicolon on windows, and a colon on unix.

Apologies, I haven't actually tried this out (and it has been ages since I used the command line) but try variations around those suggestions.

So
#1 - change directory to C:\Users\Carlos L\Tomcat\My Tomcat Projects\beer-v1\src
#2 - javac -classpath "C:\Users\Carlos L\Tomcat\apache-tomcat-8.0.28\lib\servlet-api.jar;..\classes;." -d../classes com\example\web\BeerSelect.java

or maybe

javac -classpath "C:\Users\Carlos L\Tomcat\apache-tomcat-8.0.28\lib\servlet-api.jar;classes;." -d classes -sourcepath src com\example\web\BeerSelect.java



Thanks for the response, I am not close to my pc right now, but I will try this as soon as I make it home for sure. I will tell you how it goes. Thanks!
 
Stefan Evans
Bartender
Posts: 1784
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more bit of advice. Maybe I'm old school, but file paths with spaces in them freak me out.
I would suggest creating a folder such as c:\work or c:\java to put your code in.
 
Carlos L Sanchez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I have tried this and its still not working. It shows as if the package is not found. Let me include some code this time to see if this can bring some clarity:

This is the servlet:

this is the serlvet mapping file:



and this is the code I am trying in the cmd:

C:\Users\Carlos L\Tomcat\My Tomcat Projects\beer-v1>
javac -classpath "C:\Users\Carlos L\Tomcat\apache-tomcat-8.0.28\lib\servlet-api.jar";classes:. -d classes src\com\example\web\BeerSelect.java

This obviously works as it compiles, as it is givving me the following errors:

Error: package com.example.model does not exist.
Error: cannot find symbol BeerExpert be = new BeerExpert();
^
Error: cannot find symbol BeerExpert be = new BeerExpert();
^
Error: package does not exist Iterator it = new result.iterator();
^

It seems not to be recognizing the imports.

This is my project setup:
beer-v1:
etc
...........web.xml
lib
............
src
............com
.....................example
........................................web
............................................................BeerSelect.java
........................................model
............................................................BeerExpert.java

classes
............com
.....................example
........................................web
............................................................BeerSelect.class
........................................model
............................................................BeerExpert.class
web
.............result.jsp
..............form.html

Hope this brings some clarity as to what my question is.

 
Carlos L Sanchez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so basically this solved it for me:
C:Users\Carlos L\Tomcat\My Tomcat Projects\beer-v1>
-classpath "C:\Users\Carlos L\Tomcat\apache-tomcat-8.0.28\lib\servlet-api.jar";"C:\Users\Carlos L\Tomcat\My Tomcat Projects\beer-v1\classes";classes:. -d classes src\com\example\web\BeerSelect.java

Soo, basically you sit at the project root folder, this case the beer-v1
then execute the javac, including in the classpath the servlet-api.jar and also the path to the class to be included because of dependency.

Now, it still compiles one more error, it says package result does not exist:

Iterator it = new result.iterator();
^
Any idea why this is, i checked for a typo in my classes but i could not find anything very obvious, and the util, is included, so it shouldnt have problems with the iterator, sigh...
Any ideas?
 
Carlos L Sanchez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carlos L Sanchez wrote:Ok so basically this solved it for me:
C:Users\Carlos L\Tomcat\My Tomcat Projects\beer-v1>
-classpath "C:\Users\Carlos L\Tomcat\apache-tomcat-8.0.28\lib\servlet-api.jar";"C:\Users\Carlos L\Tomcat\My Tomcat Projects\beer-v1\classes";classes:. -d classes src\com\example\web\BeerSelect.java

Soo, basically you sit at the project root folder, this case the beer-v1
then execute the javac, including in the classpath the servlet-api.jar and also the path to the class to be included because of dependency.

Now, it still compiles one more error, it says package result does not exist:

Iterator it = new result.iterator();
^
Any idea why this is, i checked for a typo in my classes but i could not find anything very obvious, and the util, is included, so it shouldnt have problems with the iterator, sigh...
Any ideas?

The error is that I am trying to create a new instance of result erroneously by saying Iterator it = new result.iterator();
it should be Iterator it = result.iterator(); instead.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic