aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "jvm can Watch "jvm can New topic
Author

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
Sheriff

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, Foo.java can be compiled while working within the fubar directory, with a simple call to javac Foo.java. And as you've discovered, Bar.java cannot be (unless the parent directory of fubar is located in the CLASSPATH).
A sure fire way to compile the dependent Bar.java source file, would be to change the working directory to the parent of the fubar directory, and using the command
javac -classpath . fubar/Bar.java
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
http://test.javaranch.com/wiki/view?HowToSetTheClasspath
[ 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
Dirk,
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: http://aspose.com/file-tools
 
subject: jvm can't find class in same package
 
Similar Threads
"I'm super(), you're (blank)?"
package statement
package problem
Protected Behaviour
javac can't find class in peer package