我是java的新手,我有一个非常常见的编译错误。我尝试了其他解决方案,但没有任何对我有用。我必须创建一个日历,这是我的Date.java的片段:
package edu.kit.informatik.calendar;
public final class Date {
private final int year; // Placeholder value
private final int month; // Placeholder value
private final int day; // Placeholder value
public int dayOfYear;
public Date(int year, int month, int dayOfMonth){
this.year = year;
this.month = month;
this.day = dayOfMonth;
}
public DateTime atTime(Time time){
DateTime dateTime = new DateTime(this, time);
return dateTime;
}
public int getYear(){
return year;
}
public int getMonthValue(){
return month;
}
public int getDayOfMonth(){
return day;
}
public int getMonth(){
return Month.ofIndex(month);
}
在另一个名为DateTime.java的文件中,我有这样的东西:
package edu.kit.informatik.calendar;
public final class DateTime {
private final Date date; // Placeholder value
private final Time time; // Placeholder value
public DateTime(Date date, Time time){
this.date = date;
this.time = time;
}
public Date getDate(){
return date;
}
public Time getTime(){
return time;
}
public int getYear(){
return date.getYear();
}
public int getMonthValue(){
return date.getMonthValue();
}
public Month getMonth(){
return date.getMonth();
}
public int getDayOfYear(){
return date.getDayOfYear();
}
public int getDayOfMonth(){
return date.getDayOfMonth();
}
public int getHour(){
return time.getHour();
}
public int getMinute(){
return time.getMinute();
}
public int getSecond(){
return time.getSecond();
}
public String toString(){
return date.toString() + "T" + time.toString();
}
}
然后还有另一个名为Time.java的文件,但它看起来就像另外两个。
当我尝试编译DateTime.java
时C:\Users\Marcel\Documents\Programmieren\assignment01- solution\TaskE\edu\kit\informatik\calendar>javac DateTime.java
DateTime.java:12: error: cannot find symbol
private final Date date; // Placeholder value
^
symbol: class Date
location: class DateTime
DateTime.java:13: error: cannot find symbol
private final Time time; // Placeholder value
^
symbol: class Time
location: class DateTime
DateTime.java:15: error: cannot find symbol
public DateTime(Date date, Time time){
^
symbol: class Date
location: class DateTime
DateTime.java:15: error: cannot find symbol
public DateTime(Date date, Time time){
^
symbol: class Time
location: class DateTime
DateTime.java:20: error: cannot find symbol
public Date getDate(){
^
symbol: class Date
location: class DateTime
DateTime.java:23: error: cannot find symbol
public Time getTime(){
^
symbol: class Time
location: class DateTime
DateTime.java:33: error: cannot find symbol
public Month getMonth(){
^
symbol: class Month
location: class DateTime
7 errors
答案 0 :(得分:1)
当您尝试编译DateTime.java时,它对您的Date类一无所知。如果使用另一个,你需要编译它们:
javac DateTime.java Date.java
答案 1 :(得分:0)
这听起来像是类路径/构建路径问题。由于您尚未设置$ CLASSPATH且未使用-cp
选项,因此编译时应位于TaskE
目录中。
或者(如@chryslis所说)使用IDE或构建工具(如Maven或Ant)来构建,并消除手动键入javac
命令的不可预测性。