Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Class won't recognize class that exists in same directory

  1. #1
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default 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

    Java Code:
    javac -d bin src\com\skyreign\store\*.java\
    It compiles successfully for some reason.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,804
    Rep Power
    19

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default 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:

    Java Code:
    C:/Programming/Java/> javac -d bin src\com\skyreign\store\Item.java

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,804
    Rep Power
    19

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    epilisantosha07@gmail.com is offline Santosha Epili
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Default Re: Class won't recognize class that exists in same directory

    make both the classes as public visibility.

  6. #6
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Class won't recognize class that exists in same directory

    Quote Originally Posted by Tolls View Post
    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.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Class won't recognize class that exists in same directory

    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

    Java 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:

    Java Code:
    java -cp bin com.skyreign.store.Storefront
    Last edited by pbrockway2; 07-04-2012 at 03:26 AM.
    awinston likes this.

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,804
    Rep Power
    19

    Default Re: Class won't recognize class that exists in same directory

    Quote Originally Posted by epilisantosha07@gmail.com View Post
    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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2011, 11:55 AM
  2. ClassNotFoundException error for a class that exists
    By dahaka2011 in forum Sun Java Wireless Toolkit
    Replies: 1
    Last Post: 04-01-2011, 02:44 AM
  3. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  4. getting class to recognize variables from another class
    By shadycharacter in forum New To Java
    Replies: 1
    Last Post: 04-26-2010, 10:14 PM
  5. Checking if file exists in a directory
    By achilles in forum New To Java
    Replies: 7
    Last Post: 08-10-2009, 02:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •