• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head first Jsp & servlets Chap 3: Compilation issue

 
Mahalakshmi Chandru
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Im trying to compile the second version of the servlet that calls the method in the model class. But the compiler says that the model class's package doesnt exist though i have imported the package properly. Servlet code and the model class code is given below:

SERVLET CLASS : BeerSelect.java
path : src/com/example/web/BeerSelect.java




MODEL CLASS: BeerExpert.java
path : src/com/example/model/BeerExpert.java




Compliation command:

BeerAdvice$ javac -d classes src/com/example/web/BeerSelect.java

src/com/example/web/BeerSelect.java:3: package com.example.model does not exist
import com.example.model.*;
^

Please advice.Thanks.
 
Swastik Dey
Rancher
Posts: 1643
6
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its a classpath issue, your classpath should map to src folder.
 
Adrian Precub
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can always use eclipse. It solves a lot of path related issues
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Importing the class is not enough. Make sure your classpath is set right.
 
Mahalakshmi Chandru
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nabila Mohammad wrote:Importing the class is not enough. Make sure your classpath is set right.


Hello All,
Thanks for your replies. It worked !! and im able to compile the class now . But, could anybody explain why we need to tell the compiler the path of the model class eventhough we have imported the class in the source code?
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good job!

From the way i think,
Importing the class means you are going to use the imported class in your current file...
But the compiler does not know where to look for the class you have imported.
So in that case ,you have to set the classpath so it can find the location of the file you have imported.

Hope this helps.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, could anybody explain why we need to tell the compiler the path of the model class eventhough we have imported the class in the source code?

Didn't you learn this when studying for SCJP ?
 
Mahalakshmi Chandru
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nabie and Chris. Its been a while since i did my SCJP. Though i had this kind of understanding, didnt click right away..
 
Ichigo Nyan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I have the same problem with this code

I know the problem is classpath issue

but I don't know how to key the right command in cmd(my OS is windows XP)

could anybody teach me?

my BeerSelect.java is located in C:\Program Files\Java\jdk1.6.0\MyProjects\beerV1\src\com\example\web
and BeerExpert.class is C:\Program Files\Java\jdk1.6.0\MyProjects\beerV1\classes\com\example\model

I'm so sorry for my poor English(I lived in Taiwan)

but I really wanna to know how to key the right classpath in cmd

thanks ><"
 
Mahalakshmi Chandru
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You can set the classpath as below:


C:\Program Files\Java\jdk1.6.0\MyProjects\beerV1> set CLASSPATH=%CLASSPATH%;C:\Program Files\Java\jdk1.6.0\MyProjects\beerV1\src;

 
Ichigo Nyan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much,Mahala

I've finished this program

I'll try my best to learn classpath ><~

Thank you(*99)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic