Re: Help with -classpath and packages



dlarsson wrote:
Okay folks, I thought I was doing something very, very simple,
but I cannot seem to get this to work at all.

Can anyone identify what I am doing wrong here-?

*snip*


---
So I try this:

javac -classpath . TestCar.java

I get the error:
TestCar.java:8: cannot access Car
bad class file: .\Car.java
file does not contain class Car
Please remove or make sure it appears in the correct subdirectory of the classpath.
---

Well, the Car.java file should really be placed inside a directory called com/example/rentalcar
This is why it tells you to place it in the correct subdirectory,
or at least I think it is.
Usually you'd have a structure sort of like this:

projectdir/src/com/example/rentalcar/Car.java

projectdir/build/com/example/rentalcar/Car.class

When compiling from the src dir, just set the -d to ../build (backslash on windows, obviously).
If this doesn't work then I don't know,
I'd recommend you use NetBeans or Eclipse to do your projects though, it would simplify these tasks a lot.

Now if I try to use the fully qualified path to the package itself (I know this shouldn't work)

javac -classpath C:\Derek\java\com\example\rentalcar TestCar.java

I get the error:
TestCar.java:2: package com.example.rentalcar does not exist
import com.example.rentalcar.*;
TestCar.java:8: cannot access Car
bad class file: C:\Derek\java\com\example\rentalcar\Car.class
class file contains wrong class: com.example.rentalcar.Car

Obviously because there's no subdirectory com/example/rentalcar
.



Relevant Pages

  • Re: File will not compile
    ... > net about creating your first program. ... I put the code in notepad, ... > saved it as MyProgram.java and ran javac. ... It created a class file, ...
    (comp.lang.java.programmer)
  • Re: How Javac works
    ... >>date but B is not and you compile A.java then C will not get rebuilt. ... > subtle -- some class file it depends on has changed, ... > Javac will handle obvious, but not the subtle, right? ...
    (comp.lang.java.programmer)
  • Re: Why does javac not check timestamps?
    ... If the class file is out of date, javac recompiles the source file and uses the updated class file. ... What javac doesn't do is chase other classes which are dependent on B if B/Y is recompiled by the rule above. ...
    (comp.lang.java.programmer)
  • Re: cannot resolve symbol
    ... vvs.java:275: cannot resolve symbol ... Entry me = VMapping.elementAt; ... javac doesn't automatically look for class files in the current ... Make sure the .class file is in this ...
    (comp.lang.java.programmer)