Class can't find another class in the same directory

Z

zburnham

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
 
R

Rhino

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.
From the sounds of it, you are working in the file system and
compiling/executing from the command line; you wouldn't likely be having
_these_ problems in an IDE, although you'd probably have _other_ problems
;-)

It sounds like you basically have things right in terms of how you're
organizing the code; both source files are in the same directory and you
have put the same package statement in each source file.

I'm not clear though about something. Is this problem happening when you are
compiling the first class, the one that refers to the second class? Or when
you are executing the first class?

Are you aware that class names are case-sensitive? If you've named the
second class Bar and you try to invoke it in the first class by
instantiating BAR, you will find that it never works. That sounds
suspiciously like the situation you are facing. Verify that any
instantiations of a given class always match the class name EXACTLY,
INCLUDING CASE. So if you define a class called Bar, the instantiation
should be: Bar myBar = new Bar();
 
H

hiwa

Dont write import my.path.*; on those sourc files.
-----------------------------------------------------------------------
package my.path;

public class Abc{

public static void main(String[] args){
Xyz xyz;

xyz = new Xyz();
xyz.print();
}

}

------------------------------------------------------------

package my.path;

public class Xyz{

public void print(){
System.out.println("I am a Xyz");
}
}

--------------------------------------------------
If you are doing your Java work in the my/path
directory, do:

javac *.java

then,

java -cp ../../ my.path.Abc
 
N

Nigel Wade

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?
 
Z

zburnham

The problem is happening when I try to compile the first class. I have
verified that capitalization matches in my attempt to call the second
class.
 
Z

zburnham

That doesn't answer one question, however. If I don't use an import
statement, shouldn't the classloader look in ./ for that class anyway?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top