• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Harinath subu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure that the class "BeerExpert" is on classpath.
 
Ruben Guillen
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Harinath subu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    any one to help me here. waiting for reply, all methods failed.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic