aspose file tools*
The moose likes Beginning Java and the fly likes javac can't find package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "javac can Watch "javac can New topic
Author

javac can't find package

Patrick Smith
Ranch Hand

Joined: Oct 08, 2007
Posts: 35
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());
}
}
}
***
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41830
    
  63
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


Ping & DNS - my free Android networking tools app
Patrick Smith
Ranch Hand

Joined: Oct 08, 2007
Posts: 35
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
Marshal

Joined: Mar 22, 2005
Posts: 41830
    
  63
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

Joined: Oct 08, 2007
Posts: 35
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
Marshal

Joined: Mar 22, 2005
Posts: 41830
    
  63
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

Joined: Oct 08, 2007
Posts: 35
that says it all, thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javac can't find package