aspose file tools*
The moose likes Servlets and the fly likes Problem in accessing model in diffeerent package(Servlet-HFS) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem in accessing model in diffeerent package(Servlet-HFS)" Watch "Problem in accessing model in diffeerent package(Servlet-HFS)" New topic
Author

Problem in accessing model in diffeerent package(Servlet-HFS)

Harinath subu
Greenhorn

Joined: Aug 03, 2009
Posts: 5


I located the following servlet code E:\src\com\example\web\BeerSelect.java






When i Compile the following Servlet from
e:\src>javac C:/Tomcat/lib/servlet-api.jar com/example/web/Beerselect.java


comiler tells me that
import com.example.model.BeerExpert class does not exist

please could any one suggest me to get the classfile
i took this code from Head First Servlet Book Chapter 3

Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Make sure that the class "BeerExpert" is on classpath.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Ruben Guillen
Greenhorn

Joined: Sep 02, 2009
Posts: 28
Dear Harinath

Just as background, Java needs to have all necessary classes during compilation time, the CLASSPATH is important in helping the compiler finds all necessary classes. Haven said this, the there are two things to take care of:

1) Get the right library with the classes you need (You already ask this question). As indicated in Java website you can get the reference implementation downloading the Java EE 5 SDK. Another option is to get a Servlet Container as Tomcat

2) Set your library on the classpath in order your compiler to find it.
something like CLASSPATH=%CLASSPATH%;c:\lib\servlet.jar

Regards.


SCJP, OCMJD
Harinath subu
Greenhorn

Joined: Aug 03, 2009
Posts: 5
then Could you tell me step by step procedure please..
i am using tomcat in c:\tomcat
i am breaking my head since last light.... yet i cant get the solution to move further in HFS book ... please provide me the solution...
Ruben Guillen
Greenhorn

Joined: Sep 02, 2009
Posts: 28
Dear Harinath

I would recommend that you address this question on the Servlet forums.

In order to advance with you problem. Keep in mind the following:

1) In order to execute you servlet you will require to deploy on a Servlet Container. You already has Tomcat, which is a Servlet Container.

2) You will need to create a web descriptor to register you servlet (web.xml)

3) deploy you servlet application on Tomcat and provide the correct URL to access you servlet.

Read the following link to get an idea of what you trying to do here

Regards.



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39416
    
  28
Ruben Guillen wrote:Dear Harinath

I would recommend that you address this question on the Servlet forums.
Agree. Moving thread.

  • Does it give any instructions in the book which you haven't followed? If there are instructions, you should follow them to the letter. The most likely cause of your problems is that you have not quite followed their instructions exactly.
  • There are some Head First examples which are incomplete, eg Party in Head First Java, so you ought to check that it tells you that BeerExpert can be executed.
  • Search for BeerExpert here on JavaRanch because that question comes up frequently.
  • When you get to the servlets forum, have a look at the FAQ there; there is bound to be something useful in that.
  • If you need to set up a CLASSPATH, always do it with the -cp options or the set options on your terminal/command prompt. Details about -cp on this page, and more about the CLASSPATH here. Note both those links are specific to Windows.
  • Never set a system CLASSPATH unless you have difficulty executing a "HelloWorld" program.
  • Atif Musaddaq
    Greenhorn

    Joined: Sep 21, 2008
    Posts: 8
    I am facing the same problem.

    I already set my classpath like this ".;C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar;F:\MyProjects\beerV1\src\com\example\model\BeerExpert.class"

    In F:\MyProjects\beerV1\src\com\example\model folder i have BeerExpert.java which is successfully compiled.

    package com.example.*;

    import java.util.*;
    @SuppressWarnings("unchecked")

    public class BeerExpert {
    public List getBrands(String color){
    List brands = new ArrayList();
    if (color.equals("amber")) {
    brands.add("Jack Amber");
    brands.add("Red Moose");
    }
    else {
    brands.add("Jail Pale Ale");
    brands.add("Gout Stout");
    }
    return(brands);
    }
    }


    And Now in F:\MyProjects\beerV1\src\com\example\web I have my BeerSelect.java

    package com.example.web;

    import com.example.model.*;
    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 {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("Beer Selection Advice
    ");
    String c = request.getParameter("color");

    BeerExpert be = new BeerExpert();
    List result = be.getBrands(c);
    Iterator it = result.iterator();
    while(it.hasNext()) {
    out.println("
    try: " + it.next());
    }
    }
    }


    and when ever i try to compile it i have following error:

    F:\MyProjects\beerV1\src\com\example\web>javac BeerSelect.java
    BeerSelect.java:3: package com.example.model does not exist
    import com.example.model.*;
    ^
    BeerSelect.java:16: cannot find symbol
    symbol : class BeerExpert
    location: class com.example.web.BeerSelect
    BeerExpert be = new BeerExpert();
    ^
    BeerSelect.java:16: cannot find symbol
    symbol : class BeerExpert
    location: class com.example.web.BeerSelect
    BeerExpert be = new BeerExpert();
    ^
    3 errors

    F:\MyProjects\beerV1\src\com\example\web>
    Atif Musaddaq
    Greenhorn

    Joined: Sep 21, 2008
    Posts: 8
    any one to help me here. waiting for reply, all methods failed.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Problem in accessing model in diffeerent package(Servlet-HFS)