File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes jvm can't find class in same package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "jvm can Watch "jvm can New topic

jvm can't find class in same package

Bob Chandler
Ranch Hand

Joined: Jan 13, 2004
Posts: 33
Hey ppl,
picture this....
I have 3 java source files each starting with the same package declaration, and each defining a separate class (ClassOne, ClassTwo & ClassThree).
They are saved in the same directory bearing the package name.
ClassOne compiles fine.
ClassTwo and ClassThree use instances of ClassOne and will not compile.
(there is an error for every mention of the ClassOne instance)
Does anyone know why the compiler can't see ClassOne?
Cheers, Bob
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
It's likely a CLASSPATH problem. The parent directory of your package directory needs to somehow be included in the CLASSPATH value.
Consider the following class definitions, where each class is defined in seperate source files, named the same as the classes, located in a directory named fubar.Sure, can be compiled while working within the fubar directory, with a simple call to javac And as you've discovered, cannot be (unless the parent directory of fubar is located in the CLASSPATH).
A sure fire way to compile the dependent source file, would be to change the working directory to the parent of the fubar directory, and using the command
javac -classpath . fubar/
The . instructs the Java compiler to include the current working directory in the CLASSPATH. Since this directory contains the fubar directory (aka the fubar package), the fubar.Foo class definition was located during compilation.
Note that it's possible that you don't need to explicitly specify the current working directory as demonstrated, since it may already be part of your CLASSPATH value.
For more information on setting the CLASSPATH, take a look at
[ March 17, 2004: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Bob Chandler
Ranch Hand

Joined: Jan 13, 2004
Posts: 33
you were absolutely right with the classpath solution!
Everything was in order as regards system settings, but
I was trying to access my code from the command line in
completely the wrong way.
Thanks so much for your help
I agree. Here's the link:
subject: jvm can't find class in same package
It's not a secret anymore!