Two Laptop Bag*
The moose likes Servlets and the fly likes Compile error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Compile error" Watch "Compile error" New topic
Author

Compile error

Suja Mudunuri
Greenhorn

Joined: Jul 28, 2004
Posts: 14
I have 2 classes as below from book HFS and JSP page 84.

package com.example.model;
import java.util.*;

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);
}
}
This class compiles fine
I Am importing the above class in the following Servlet class

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<br>");
String c = request.getParameter("color");

BeerExpert be = new BeerExpert();
List result = be.getBrands(c);
Iterator it = result.iterator();
while(it.hasNext()){
out.print("<br>try: " + it.next());
}
}
}
I keep getting the following error
src/com/example/web/BeerSelect.java:3: package com.example.model does not exist
import com.example.model.*;
^
src/com/example/web/BeerSelect.java:17: cannot find symbol
symbol : class BeerExpert
location: class com.example.web.BeerSelect
BeerExpert be = new BeerExpert();
^
src/com/example/web/BeerSelect.java:17: cannot find symbol
symbol : class BeerExpert
location: class com.example.web.BeerSelect
BeerExpert be = new BeerExpert();
^
3 errors

I do have a class in this path C:\Sujatha\MyProjects\beerV1\classes\com\example\model

Can somebody please help be with what I am doing wrong?


Sujatha Mudunuri<br />SCJP 1.4<br />(Preparing SCWCD 1.4)
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Check your classpath


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Suja Mudunuri
Greenhorn

Joined: Jul 28, 2004
Posts: 14
what do I need to have in my classpath?
Do I need to put my package path? C:\Sujatha\MyProjects\beerV1\classes\com\example\model
Nikhil Menon
Ranch Hand

Joined: Nov 22, 2004
Posts: 70
Hi,

No class path should be your parent directory

set path to here C:\Sujatha\MyProjects\beerV1\classes\

Then refer your package relative to this like com.example......

Nikhil Kanjulli Menon
SCJP1.4
Suja Mudunuri
Greenhorn

Joined: Jul 28, 2004
Posts: 14
Thank You Nikhil for the immediate response. I am able to compile now.
Tim Meyers
Greenhorn

Joined: Feb 25, 2005
Posts: 2
I don't know what I'm missing here. I just can't seem to get it to compile. My compile string is "javac -classpath c:\tomcat\common\lib\servlet-api.jar -d classes src\com\example\web\BeerSelect.java" in a command window. I've been trying to resolve this for hours, and can't seem to make any progress.

Any help?
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Timothy,

What error are you getting?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

By the look of it, you also need your own classes on the classpath, not just the servlet jar. Try something like this:



No promise, but that's my guess.

Dave
Tim Meyers
Greenhorn

Joined: Feb 25, 2005
Posts: 2
That did the trick.

Thanks
Greg Helton
Greenhorn

Joined: Feb 26, 2005
Posts: 1
I suggest you change directories to src and change the compile command to

"javac -classpath c:\tomcat\common\lib\servlet-api.jar -d ..\classes com\example\web\BeerSelect.java"

I start my compile with the package directory (not from "src" above "com"). And, I find ANT helps eliminate compilation problems.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compile error
 
Similar Threads
weird problem
can't compile a servlet
can't import own model
Head First Servlet : Beer Version 2 Problem
package does not exist problem