Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

package not found

 
Nicholas Carrier
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I guess I'm mistyping something here, because I keep getting an error message when I try to compile this that says,

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

And then it gives me a similar error anytime I try to use something from the model package. Anyways here is the code of the two files and then the directory structures.

The file that won't compile

package com.wittycritics.web;

import com.wittycritics.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 Seclection 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());

} // end of while

} // end of method

} // end of class

The only file in the model directory - it compiles just fine

package com.wittycritics.model;

import java.util.*;

public class BeerExpert {

public List getBrands(String color) {
List<String> brands = new ArrayList<String>();
if (color.equals("amber")) {
brands.add("Jack Amber");
brands.add("Red Moose");
}

else {
brands.add("Jail Pale Ale");
brands.add("Gout Stout");

}

return(brands);

} // end of method

} // end of class

BeerSelect.java is in:

C:\MyProjects\beerV1\src\com\wittycritics\web

BeerExpert.java is in:

C:\MyProjects\beerV1\src\com\wittycritics\model

BeerExpert.java compiled to:

C:\MyProjects\beerV1\classes\com\wittycritics\model

but when I try to compile the BeerSelect.java file, I get the above error message.

I'm also compiling it using (I'm in directory C:\MyProjects\beerV1 when I compile it):

javac -d classes src/com/wittycritics/web/BeerSelect.java

I figure something isn't typed correctly and it's just impossible for me to see it, either that or I am missing something fundamentally easy regrading how to use packages. Anyways, any help anyone can give me would be greatly appreciated.

I'm on an Windows 2000 machine with SDK version 1.5
[ June 28, 2005: Message edited by: Nicholas Carrier ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even though you're telling javac where to find the BeerSelect.java file, it doesn't know where to find the other related files unless you tell it:

javac -d classes -classpath src src/com/wittycritics/web/BeerSelect.java
 
Nicholas Carrier
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, you lost me. So everytime I create a package, I have to add that to my CLASSPATH if I want to import it, or compile with the -classpath command? You would think that somewhere in the Head First series they would have explained this to me. Anyways, I'll give it a try, and let you know.

Thanks for your help.
 
Nicholas Carrier
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so I added the file to my CLASSPATH, because if I just use that one file for my CLASSPATH when compiling, it won't compile because it's also looking for servlets.

Anyways, here is my CLASSPATH now:

C:\Apache\Tomcat5.5\common\lib\servlet-api.jar;C:\MyProjects\beerV1\classes\com\wittycritics\model\BeerExpert.class

And I still receive the same error when I try to compile the code.

Thanks again for your help, I really appreciate it.

P.S. I only use bold type to make it easier to read, I'm not screaming or getting mad or anything. Sometimes it easier on the eyes when certain things are a little different.
[ June 28, 2005: Message edited by: Nicholas Carrier ]
 
Ryan McGuire
Ranch Hand
Posts: 1057
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need individual class files or even packages enumerated on your classpath, just the direcotries that contain them.

For instance if the class file com.wittycritics.model.BeerExpert is in C:\MyProjects\beerV1\classes\com\wittycritics\model\BeerExpert.class then you need to put C:\MyProjects\beerV1\classes on your classpath. Javac will automatically convert the package and class names into directory and file names.

You can have a bunch of packages rooted in a single direcotry on the classpath. For instance, C:\MyProjects\beerV1\classes might contain com/wittycritics, com/somethirdparty, org/someoneelse, etc.

When Ernest gave you the command...

javac -d classes -classpath src src/com/wittycritics/web/BeerSelect.java

...the src/com/wittycritics/web/BeerSelect.java argument wasn't part of the classpath; it was telling javac what file to compile. Only src was part of the classpath, and it was needed so that javac could find BeerExpert when compiling BeerSelect.

Given that you need some apache classes, your classpath should be...

C:\Apache\Tomcat5.5\common\lib\servlet-api.jar;C:\MyProjects\beerV1\classes
 
Nicholas Carrier
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is why the ranch is the best! Thanks again. Although I'm still relativley surprised that this wasn't explained anywhere, in anything I've read so far, which makes me wonder if I'm reading the right material, lol. Anyways, off to the servlet world I go!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic