File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes I Can't reference a class that's in the same package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I Can Watch "I Can New topic
Author

I Can't reference a class that's in the same package

Tom Purl
Ranch Hand

Joined: May 24, 2002
Posts: 104
I have the following two classes. First, I have the Field class.

Next, I have the Record class that references the Field class.

Both classes are in the same folder, which is /home/tom/Dev/Java/ff2db/flatfile/data. My classpath variable contains the following text:
CLASSPATH=$CLASSPATH:.:$HOME/Dev/Java/Scratch
... so I don't have to use the "-classpath ." switch if I'm compiling a class in the current directory.
When I compile the Field class, I don't have any problems. However, I'm trying the following command to compile the Record class:
[tom@localhost /home/tom/Dev/Java/ff2db/flatfile/data] javac Record.java

Here's what I've tried to fix this problem.
  • I tried to compile the class from the /home/tom/Dev/Java directory. One newsgroup posting said that this may work. It didn't.
  • I tried to explicitly reference the Field class by using the folloiwng signature: public void addField(ff2db.flatfile.data.Field f). This also didn't work.


  • Since both classes are in the same package, I don't know why I'm having this problem. Any help would be greatly appreciated!
    Thanks,
    Tom
    [ December 08, 2002: Message edited by: Tom Purl ]

    Tom Purl<br />SCJP 1.4
    Maulin Vasavada
    Ranch Hand

    Joined: Nov 04, 2001
    Posts: 1871
    hi Tom,
    u have written that you have your classes in,
    /home/tom/Dev/Java/ff2db/flatfile/data
    folder, but in the CLASSPATH u have
    $HOME/Dev/Java/Scratch
    i guess that is the problem. don't you have to have $HOME/Dev/Java in the classpath instead?
    regards
    maulin
    Tom Purl
    Ranch Hand

    Joined: May 24, 2002
    Posts: 104
    I also have the "." directory in my classpath. This means that when I compile a program, it adds the current directory to my classpath. So if my java file is in the /foo/bar directory and I use the following command:
    [/foo/bar tom@localhost] javac test.java
    or in Windows...
    C:\foo\bar> javac test.java
    ...the javac program should add the /foo/bar directory to the classpath.
    Thanks for the quick reply Maulin! Does this make the problem clearer? Does anyone else see what could be wrong?
    Thanks again!
    [ December 09, 2002: Message edited by: Tom Purl ]
    Tom Purl
    Ranch Hand

    Joined: May 24, 2002
    Posts: 104
    In case anyone else was trying to figure this out, I think I've finally figured this out.
    All I had to do was compile the program from the directory that was the parent of the first element in the package name. So if my package name was tom.test, and my files were in C:\tom\test, I needed to compile my program from C:\. I tried this before on my Linux box but it didn't work. I guess either I forgot something or this behaves differently on Linux.
    Maulin Vasavada
    Ranch Hand

    Joined: Nov 04, 2001
    Posts: 1871
    Hi Tom,
    well, it does put current directory but if u've a package structure then it doesn't really work the way u expect it to be....
    finally u got urself that what u should do...i ALWAYS use parent dir of the package to compile...
    if my package is maulin.apps and i have,
    c:\java\test\maulin\apps
    then i do cd c:\java\test
    then, javac maulin\apps\*.java

    now the current dir that is "java\test" is in the classpath where my package resides "maulin.apps" so i don't need to do any explicit settings...
    regards
    maulin
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: I Can't reference a class that's in the same package