Re: Class can't find another class in the same directory
zburnham@gmail.com wrote:
I'm trying to build a class that references a second class that I've
made. No matter what I seem to do, I get a "cannot find symbol" error
referring to the second class when I go to build it.
The class files are in the same directory, which for the sake of
argument, we'll call /my/path . That is, they're in a folder called
"path" which is inside a folder called "my". I've added "package
my.path;" to the beginning of each class.
I've tried using "import my.path.*;", tried adding the root of this
path (that is, the folder that "my" is in) to my CLASSPATH, tried
referring to the other class as both my.class.Foo and Foo, nothing
seems to work.
It sounds like a CLASSPATH problem, I know, but I can't figure out what
it is that's wrong.
Any suggestions are welcome.
Z
If they belong to the same package there is no need to import anything.
What is probably causing your problem is that you are trying to compile the code
whilst within the package sub-directory. The javac classloader cannot find the
necessary class files relative to your current location.
To follow your example, you have the package class hierarchy my.path,
represented by the directory hierarchy my/path. When the classloader tries to
locate a class file, for example a.class, in package my.path it will look for
it in the file my/path/a.class relative to the current location. So if you are
compiling b.java, which refers to a.class, whilst in the directory my/path the
class loader will not find my/path/a.class.
To compile b.java you need the directory which contains the top level "my" of
my/path to be on the classpath. Either cd to that directory and javac
my/path/b.java, or include the directory in the classpath as javac -cp ../..
b.java
Fun, isn't it?
--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555