This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes package com.example.model.* does not exist ERROR head first servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "package com.example.model.* does not exist ERROR head first servlets " Watch "package com.example.model.* does not exist ERROR head first servlets " New topic
Author

package com.example.model.* does not exist ERROR head first servlets

srinivas venkat
Greenhorn

Joined: Aug 19, 2009
Posts: 8
Hi everyone i am newbie to this community.I am reading head first servlets .I got an error showing package com.example.model.*; doesnot exist .........
I set my environmental variable classpath as C:\Program Files\Java\jdk1.6.0\lib\tools.jar;C:\Program Files\Java\jdk1.6.0\lib\dt.jar;.;C:\Program Files\Apache Software Foundation\Tomcat 5.0\common\lib\servlet-api.jar;%CLASSPATH%;

I can compile BeerExpert.java but i cannot compile BeerSelect.java file

my directory structure for BeerSelect.java is C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\chanti3\WEB-INF\classes\com\example\web\BeerSelect.java

my directory structure for BeerExpert.java is C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\chanti3\WEB-INF\classes\com\example\model\BeerExpert.java


I got the error in the line import com.example.model.*;

can any1 help me?
waiting for your reply

thank you
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

Hi Srinivas, Welcome to JavaRanch . Few things you should get familiar first.
my directory structure for BeerSelect.java is C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\chanti3\WEB-INF\classes\com\example\web\BeerSelect.java

my directory structure for BeerExpert.java is C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\chanti3\WEB-INF\classes\com\example\model\BeerExpert.java

You should never put your source files (for servlets, classes etc..) inside application server as you have done here. Choose different directory outside and put only the compiled class files inside.

I set my environmental variable classpath as C:\Program Files\Java\jdk1.6.0\lib\tools.jar;C:\Program Files\Java\jdk1.6.0\lib\dt.jar

You don't need to set these here.

I got the error in the line import com.example.model.*;

If one of your source files uses any other class file which you compiled those should be accessible in order to compile the class (which is done usually setting a CLASSPATH entry to those packages).

SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
srinivas venkat
Greenhorn

Joined: Aug 19, 2009
Posts: 8
Thanks for your reply

I ill keep my source files in another directory from now onwards


If one of your source files uses any other class file which you compiled those should be accessible in order to compile the class (which is done usually setting a CLASSPATH entry to those packages)

I didnt get your point ......how can i set classpath entry ? how can i solve this problem?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

......how can i set classpath entry ? how can i solve this problem?


That is, add a CLASSPATH entry just as you have done for the others above pointing to the top level directory which contains your compiled classes (with the correct package heirarchy). This case the directory contains com/example/model.
srinivas venkat
Greenhorn

Joined: Aug 19, 2009
Posts: 8
Thanks for your help Vijitha Kumara but still the same errors continues package com.example.model doesnot exist ................ i jus changed directories like what you have said...................

i am compiling using the following command

E:\projects\p3>javac -classpath "C:\Program Files\Apache Software Foundation\Tom
cat 5.0\common\lib\servlet-api.jar";"E:\projects\p3\classes\com\example\model";classes:. -d classes src\com\example\web\BeerSelect.java

and my changed directory structure is
E:\projects\p3\src\com\example\model\BeerExpert.java------------this one is compiling and creating .class file at E:\projects\p3\classes\com\example\model\BeerExpert.class
E:\projects\p3\src\com\example\web\BeerSelect.java

the source code for BeerSelect.java is

package com.example.web;
import com.example.model.*; ----------error in importing this
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;


public class BeerSelect extends HttpServlet
{
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
String c=request.getParameter("color");
BeerExpert be=new BeerExpert();
List result=be.getBrands(c);

response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("Beer Selection Advice <br>");

Iterator it=result.iterator();
while(it.hasNext())
{
out.println("<br>try:"+it.next());
}
}
}

help me out please
srinivas venkat
Greenhorn

Joined: Aug 19, 2009
Posts: 8
any one there?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

srinivas venkat wrote:any one there?

Please read this and show some patience.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

srinivas venkat wrote:i am compiling using the following command
E:\projects\p3>javac -classpath "C:\Program Files\Apache Software Foundation\Tom
cat 5.0\common\lib\servlet-api.jar";"E:\projects\p3\classes\com\example\model";classes:. -d classes src\com\example\web\BeerSelect.java


No. I mean you have to add the directory which contains the "com\example\model" to the CLASSPATH, that is "E:\projects\p3\classes\". Try with this & "C:\Program Files\Apache Software Foundation\Tomcat 5.0\common\lib\servlet-api.jar" as the -classpath entries (remove all the others) and It should compile.

[EDIT]
Or even if you include "classes" in the classpath (with the servlet.jar ofcourse) it should work as it seems you are having the classes directory in the current directory (i.e: the one you are compiling from)
[/EDIT]
srinivas venkat
Greenhorn

Joined: Aug 19, 2009
Posts: 8
That solved my problem........ i am a beginner started learning servlets and jsps so dont mind if i ask any silly doubts.......Thanks a lot for your help vijitha Kumara ....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: package com.example.model.* does not exist ERROR head first servlets
 
Similar Threads
Regarding Package
Head First Servlets and JSP (problem on pg-84,85)
head first servlets & jsp(example on pg 84,85)
package help
Package does not exist ?