java 在同一个包和目录中找不到符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32598120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:28:36  来源:igfitidea点击:

Cannot find symbol in same package and directory

javacompilationsymbols

提问by John

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

我有两个课程,课程和课程。它们都在同一个包和同一个目录中。

Offering.java:

提供.java:

package assignment02;

public class Offering implements Comparable<Offering> {
    private Course course;
    private int CRN;
    private int semester;

    public Offering(Course course, int CRN, int semester) {
        this.course = course;
        this.CRN = CRN;
        this.semester = semester;
    }

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

    public int getCRN() {
        return CRN;
    }

    public int getSemester() {
        return semester;
    }

    public int compareTo(Offering other) {
        if(other == null) return - 1;
        return semester - other.semester;
    }
}

Course.java:

课程.java:

package assignment02;

public class Course {
    private String name;
    private String rubric;
    private String number;
    private int numCredits;

    public Course(String name, String rubric, String number, int numCredits) {
        this.name = name;
        this.rubric = rubric;
        this.number = number;
        this.numCredits = numCredits;
    }

    public String getName() {
        return name;
    }

    public String getRubric() {
        return rubric;
    }

    public String getNumber() {
        return number;
    }

    public int getNumCredits() {
        return numCredits;
    }
}

When I try to compile Offering, I get the errors:

当我尝试编译产品时,出现错误:

D:\CS 140\assignment02>javac Offering.java
Offering.java:4: error: cannot find symbol
    private Course course;
            ^
   symbol:   class Course
   location: class Offering

and

Offering.java:8: error: cannot find symbol
    public Offering(Course course, int CRN, int semester) {
                    ^
   symbol:   class Course
   location: class OfferingOffering.java:8: error: cannot find symbol

I know that the error means the compiler is unable to do anything with 'Course,' but I don't really know why. I also know that it will end up being something incredibly obvious, but I just can't seem to figure it out. Any help would be really appreciated.

我知道该错误意味着编译器无法对“课程”执行任何操作,但我真的不知道为什么。我也知道它最终会变得非常明显,但我似乎无法弄清楚。任何帮助将非常感激。

回答by MadProgrammer

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

将目录更改为assignment02. 然后你应该可以使用

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 Courseclass in the assignment02package FROM your current directory (so when your in the assignment02directory, it's effectively trying to look in assignment02/assignment02, which obviously isn't right)

编译器正在从您当前的目录中查找包中的Courseassignment02(所以当您在assignment02目录中时,它实际上是在尝试查找assignment02/assignment02,这显然是不对的)

While this will correct you current problem you will then get...

虽然这将纠正您当前的问题,但您将获得...

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

which will need to correct

这将需要纠正

回答by Doug Warren

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

您还可以使用“-classpath”参数进行编译,并将其值设置为父目录,如下所示:

javac -classpath .. Offering.java

回答by jhn

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

你需要先编译Course.java——Offering.java依赖它,因为你引用了它。还,

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

should be

应该

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