• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error when compiling the servelet  RSS feed

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone

I am still a newbie to servlets and JSP and I am reading head first jsp & servlets book these days .
I am trying to do one of its exercises which involves retrieving data in an arraylist (this is the model) and display it to the user depending on an option user selects

When I try to compile the servlet it says a package com.example.model does not exist (This is the package I am importing in my servlet)

I am following MVC pattern in this exercise and this is my directory structure

Development folder exists in the desktop
beerV1
- src
-com (sub folder of src)
-example (sub folder of com)
-model (sub folder of example)
BeerExpert.java (This simply returns an arraylist and it does not have any compile errors. I already compile it successfully)
-web (sub folder of example)
BeerSelect.java (This is the servlet and I get the error when I compile this servlet)
- classes (sub folder of src)
- etc (sub folder of src)
-lib (sub folder of src)
-web (sub folder of src)

Deployment folder exists in C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps

When I compile BeerSelect.java servlet compiler complains "package com.example.web" does not exist. This is because I am importing BeerExpert.java file in my servlet to invoke a method.

This is what I typed in cmd prompt
javac -classpath /"C:/Program Files/Apache Software Foundation/Tomcat 8.0"/lib/servlet-api.jar;classes:. -d classes src/com/example/web/BeerSelect.java

Can anybody explain me where I have gone wrong?
Is this due to classpath?

Thanks




 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gihan Madushanka wrote:compiler complains "package com.example.web" does not exist. This is because I am importing BeerExpert.java file in my servlet to invoke a method.

But the BeerExpert class is in the com.example.model package.
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Gihan Madushanka wrote:compiler complains "package com.example.web" does not exist. This is because I am importing BeerExpert.java file in my servlet to invoke a method.

But the BeerExpert class is in the com.example.model package.

Sorry it is a mistake that I did when I was typing. Actually what compiler says is "package com.example.model" does not exist
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not know why I cannot edit my question
I have done a mistake when mentioning compiler error

Actual error is
When I compile BeerSelect.java servlet compiler complains "package com.example.model" does not exist. This is because I am importing BeerExpert.java file in my servlet to invoke a method.
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Moved to the Beginning Java forum.

ok. Sorry. I am going through similar threads and understood this is a problem with my classpath. Please help
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After several attempts I could get it worked. I am posting this answer so it will be helpful for a future reader.
This issue happens when you have not specified classpath properly. So that JVM cannot import your package com.example.model

Steps to resolve
Add "C:/......../Project1/beerV1/src" in your classpath

This will ultimately look like as below

C:\Users\User\Desktop\JSP\beerV1>javac -classpath "C:/Program Files/Apache Software Foundation/Tomca
t 8.0"/lib/servlet-api.jar;C:/Users/User/Desktop/JSP/beerV1/src;classes:. -d classes src/com/example
/web/BeerSelect.java

Second class path is in bold.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just noticed the actual problem. You don't need to add the src directory to your classpath.
Assuming that when you compiled the BeerExpert file the class file was created in the classes/com/example/model directory, then your original classpath was almost correct.

"C:/Program Files/Apache Software Foundation/Tomcat 8.0"/lib/servlet-api.jar;classes:.

You are using the wrong separator between classes and .
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:I've just noticed the actual problem. You don't need to add the src directory to your classpath.
Assuming that when you compiled the BeerExpert file the class file was created in the classes/com/example/model directory, then your original classpath was almost correct.

"C:/Program Files/Apache Software Foundation/Tomcat 8.0"/lib/servlet-api.jar;classes:.

You are using the wrong separator between classes and .

I am using windows. So I think I must use ; . is not it right?
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, use ; on Windows®
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Yes, use ; on Windows®

So what is the error in my original post. It already has ;.
am I right Ricthie?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know. Have you confirmed that the classpath points to the correct folders?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!