Class won't recognize class that exists in same directory
I have 2 classes (Item and Storefront) in the same package (com.skyreign.store).
Storefront references Item 4 times, and Item does not reference Storefront. Item compiles fine, but when I try to compile Storefront, I receive the "Cannot find symbol" error 4 times. These errors complain about the 4 times that Storefront references Item.
Why can't my Storefront class recognize the Item class that exists in the same directory?
And here is an even interesting development: when I compile all of the java files in the directory using
Code:
javac -d bin src\com\skyreign\store\*.java\
It compiles successfully for some reason.
Re: Class won't recognize class that exists in same directory
How are you compiling?
Where are you when you run the javac command (as in what directory)?
Because this is a classpath problem, probably caused by you attempting to compile your code from the directory the code is in without adding '-cp <path to your src directory>' to the command.
Re: Class won't recognize class that exists in same directory
I'm compiling via the command prompt using the javac command. I compile from C:\Programming\Java which contains src\com\skyreign\store\Item.java & Storefront.java.
So it looks something like this to compile Item.java:
Code:
C:/Programming/Java/> javac -d bin src\com\skyreign\store\Item.java
Re: Class won't recognize class that exists in same directory
javac -cp src -d bin src\com\skyreign\store\Item.java
Your classpath starts in the src directory, not the one above it.
Not sure why *.java works.
Re: Class won't recognize class that exists in same directory
make both the classes as public visibility.
Re: Class won't recognize class that exists in same directory
Quote:
Originally Posted by
Tolls
javac -cp src -d bin src\com\skyreign\store\Item.java
Your classpath starts in the src directory, not the one above it.
Not sure why *.java works.
Thanks for the help! Just to clarify, though...
I always thought that if if Class A and Class B exist in the same package, Class A could reference Class B without designating a classpath during compiling. I though that the directory of the class being compiled was the first or second place that Java looks when searching for referenced classes.
Re: Class won't recognize class that exists in same directory
Quote:
I though that the directory of the class being compiled was the first or second place that Java looks when searching for referenced classes.
No - aside from the "builtin" classes, the java executables (compiler and runtime etc) will look in all the places on the classpath and only in those places.
I would have thought the correct command is
Code:
javac -cp bin -d bin -sourcepath src src\com\skyreign\store\Item.java
It allows the compiler to "resolve" a class like com.skyreign.store.Item by looking in the bin directory for a directory called com, and then, within that for skyreign etc. If no class is found the compiler will compile one, finding the source file by a parallel process starting at src. Details are on the compiler's man page.
Multiple source files can be specified (like \etc\*.java) and recompiliation can also take place based on what the compiler finds in -sourcepath. Setting the classpath also makes sense when the program is run:
Code:
java -cp bin com.skyreign.store.Storefront
Re: Class won't recognize class that exists in same directory
Quote:
Originally Posted by
epilisantosha07@gmail.com
make both the classes as public visibility.
Honestly, you're not helping.
This is, what, the third time (at least) you have posted something either wrong or irrelevant to the problem at hand.