Class can't find another class in the same directory

Discussion in 'Java' started by zburnham@gmail.com, May 2, 2006.

  1. Guest

    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
    , May 2, 2006
    #1
    1. Advertising

  2. Rhino Guest

    <> wrote in message
    news:...
    > 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();

    --
    Rhino
    Rhino, May 2, 2006
    #2
    1. Advertising

  3. hiwa Guest

    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
    hiwa, May 3, 2006
    #3
  4. Nigel Wade Guest

    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 :
    Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
    Nigel Wade, May 3, 2006
    #4
  5. Guest

    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.
    , May 3, 2006
    #5
  6. Guest

    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?
    , May 3, 2006
    #6
  7. Thomas Weidenfeller, May 3, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chiranjiv choudhary via .NET 247
    Replies:
    1
    Views:
    349
    Patrice
    Apr 6, 2005
  2. ad
    Replies:
    2
    Views:
    845
  3. mike
    Replies:
    2
    Views:
    475
    Roedy Green
    Mar 15, 2008
  4. Robocop
    Replies:
    5
    Views:
    308
    Robocop
    Oct 17, 2008
  5. J2M
    Replies:
    0
    Views:
    69
Loading...

Share This Page