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

时间:2015-09-16 01:06:30

标签: java compilation symbols

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

Offering.java:

var result = [];

for (var i = 0; i < rowCount; i++) {
    var newRow = [];
    for (var j = 0; j < rowCount; j++) {
      newRow.push(0);
    }
    result.push(newRow);
}

Course.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;
    }
}

当我尝试编译产品时,我收到错误:

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;
    }
}

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

我知道错误意味着编译器无法对“课程”做任何事情,但我真的不知道为什么。我也知道它最终会变得非常明显,但我似乎无法弄明白。任何帮助都会非常感激。

3 个答案:

答案 0 :(得分:9)

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

javac assignment02\Course.java assignment02\Offering.java

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

甚至

javac assignment02\*.java

编译器正在寻找当前目录Course包中的assignment02类(所以当你在assignment02目录中时,它会有效地试图查看在assignment02/assignment02中,显然不是正确的)

虽然这会纠正你当前的问题,但你会得到......

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

需要纠正

答案 1 :(得分:0)

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

javac -classpath .. Offering.java

答案 2 :(得分:-2)

你需要编译Course.java first-Offering.java依赖它,因为你引用它。此外,

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

应该是

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