2016-11-29 49 views
-3

我是新來的java,我有一個很常見的編譯錯誤。我嘗試了其他解決方案,但沒有爲我工作。我要創建一個日曆,這裏是一個片段FRM我Date.java: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

你大概還沒有編譯你的Date類。將兩者都傳遞給'javac',或者甚至更好地使用IDE。 – chrylis

+0

正如我所說,如果這些可以解決我的問題,我沒有。 –

+0

@MarcelM。更正 - 其中一個會......但你無法弄清楚許多可能的問題/解決方案中哪一個與你相匹配。 –

回答

1

它不知道,當你試圖編譯DateTime.java您的Date類東西。如果使用另一個,則需要對它們進行編譯:

javac DateTime.java Date.java 
+0

我把它作爲回報:javac:invalid flag:Date.class –

+0

我正在編譯.java的.class instdt。當我一次編譯5個文件的時候它就起作用了。謝謝哥們! –

+0

但是我必須從現在開始一起編譯所有文件嗎? –

0

聽起來像是類路徑/構建路徑問題。由於您尚未設置$ CLASSPATH,並且您沒有使用-cp選項,因此編譯時應該位於TaskE目錄中。

或者(如@chryslis所說)使用IDE或Maven或Ant等構建工具來完成您的建築,並消除手動鍵入javac命令的不可預測性。

+0

當我在/ taskE目錄中時,cmd告訴我他找不到該文件。 –

+0

然後給出相對於你的位置的文件路徑! –

+0

但是,當我在文件的目錄中,或者我喜歡路徑時,它會發生什麼變化? –