Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

javac can't find package

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i have a simple webapp in the developperphase (as given in the book "Head First Servlets & JSP"). I have installed jdk 1.6.0.4 and jre 1.6.0.4 (public). And i have Tomcat 6.0.14 up and running.
Now, i want to compile a servlet (see code at the end between "***") but the "import" command doesn't work, as it can't find the path. Here's the message by javac:
"C:\WebAppsDev\beerV1>javac -d classes src/com/example/web/beerselect.java
src\com\example\web\beerselect.java:3: package com.example.model does not exist
import com.example.model.*;
^
1 error"
.

I have set the JAVA_HOME variable (Windows XP SP2) in the systemvariables and in the uservariables the following way: C:\Program Files\Java\jdk1.6.0_04

I am about to troubleshoot this for weeks, i can't get the thing running, thanks for a feedback,
Patrick

Servlet code:

***
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());
}
}
}
***
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All classes on which this class depend must be in the classpath. An easy way to accomplish this would to compile like this:

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

That's assuming that the other class was compiled earlier with a similar command, and is in C:\WebAppsDev\beerV1\classes
 
Patrick Smith
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, i'm getting closer.
When i enter the command you gave me, javac can't find the servlet-api.jar file, as my environment variables get overwritten.
Therefore, i've entered the command below (see between ***) and then compiled. It worked, but i have one question left: what does the last path mean ("classes")? It must be relative to where i start the javac from, as the C:.... path is not indicated. Can you explain or give me a ink where i can read that?
Kind regards, Patrick SAAR


""
set CLASSPATH=
.;
C:\Program Files\Java\jre1.6.0_01\lib\ext\QTJava.zip;
C:\Program Files\apache-tomcat-6.0.14\lib\servlet-api.jar;
C:\Program Files\Java\jdk1.6.0_04;
C:\WebappsDev;
C:\WebAppsDev\beerV1\classes\com\example\model;
classes
""

Then, i enter the following to compile Beerselect.java and javac replies the following:

C:\WebAppsDev\beerV1>javac -d classes src/com/example/web/Beerselect.java
src\com\example\web\Beerselect.java:3: package com.example.model does not exist

I've tried your proposal to enter the following, but then it can even not find servlet-api.jar and gives lots of other errors, but the error above is gone! How to proceed? Hall i add something to my "set CLASSPATH" command?
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I generally recommend not use the CLASSPATH variable, but to use the -classpath command line switch instead. It gets tiresome after a while to keep adding entries to CLASSPATH every time you start a new project. Not to mention that class names may start to conflict with one another. But to a certain degree that's personal preference.

You should not put either C:\WebAppsDev\beerV1\classes\com\example\model nor classes in the CLASSPATH variable, but instead put C:\WebAppsDev\beerV1\classes. Then the classes directory is available no matter in which directory you happen to be in.

Also, I doubt that either C:\Program Files\Java\jdk1.6.0_04 or C:\WebappsDev do any good (or anything at all, actually). It's unlikely that class files reside in either of those directories (and if they do, I'd move them elsewhere).
[ February 04, 2008: Message edited by: Ulf Dittmer ]
 
Patrick Smith
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
great, i've taken away all the uneeded an here's the smallest line that works:

"C:\Program Files\apache-tomcat-6.0.14\lib\servlet-api.jar; C:\WebAppsDev\beerV1\classes"

There's omne question left as the book indicates sth. bizarre to me. Here's the command (the "..." is up to each's own directory structure up to the Tomcat home directory):
"javac -classpath /..../tomcat/common/lib/servlet-api.jar:classes:. -d classes src/com/example/web/Beerselect.java"

What does the ":classes:." mean? When i try that, it just gives errors. And what does the "classes" behind "-d" mean?

But anyhow, it all works, this is just for a deeper understanding. I thank ou greatly for your efficient help.

Kind regards, Patrick
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What does the ":classes:." mean?


Unix-ish operating system (like Maxc OS X and Linux) use the colon to separate entries in a list of paths, whereas Windows uses the semicolon.

And what does the "classes" behind "-d" mean?


The -d switch determines the directory where class files are written. So "-d classes" means that the .class files should go into a directory called "classes" inside of the current directory. If you leave out the switch altogether, the files are written in the current directory.
 
Patrick Smith
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that says it all, thanks again.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic