Cannot find symbol in same package and directory

后端 未结 3 1044
遥遥无期
遥遥无期 2021-01-05 02:15

I have two classes, Offering and Course. They are both in the same package and the same directory.

Offering.java:

package assignment02;

public class         


        
相关标签:
3条回答
  • 2021-01-05 02:52

    You need to compile Course.java first—Offering.java depends on it because you referenced it. Also,

    public int getNumCredits() {
        return course.getNumCredits;
    }
    

    should be

    public int getNumCredits() {
        return course.getNumCredits();
    }
    
    0 讨论(0)
  • 2021-01-05 02:58

    You can also compile using the "-classpath" argument, with its value set to the parent directory, like this:

    javac -classpath .. Offering.java
    
    0 讨论(0)
  • 2021-01-05 03:02

    Change directories to the parent directory of assignment02. You should then be able to use

    javac assignment02\Course.java assignment02\Offering.java
    

    or

    javac assignment02\Course.java 
    javac assignment02\Offering.java
    

    or even

    javac assignment02\*.java
    

    The compiler is is looking for the Course class in the assignment02 package FROM your current directory (so when you're in the assignment02 directory, it's effectively trying to look in assignment02/assignment02, which obviously isn't right).

    While this will correct your current problem you will then get the following error:

    assignment02\Offering.java:15: cannot find symbol
    symbol  : variable getNumCredits
    location: class assignment02.Course
        return course.getNumCredits;
                     ^
    1 error
    

    Which will need to corrected.

    0 讨论(0)
提交回复
热议问题