首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Eclipse/MyEclipse 導入項目報錯「enum 識別符錯誤」?一鍵修復編譯器合規級別

     編輯:Java 綜合教學

問題現象

從他人導入的 Java 項目,編輯器大面積紅錯,提示:

Syntax error on token "enum", @ expected
enum cannot be resolved to a type

但代碼明明使用了 JDK 5+ 的 enum 關鍵字。

根因

項目編譯器合規級別(Compiler Compliance Level)設置為 **1.4 或 1.5 以下**,導致編譯器不識別 enum、@Override、泛型、自動拆箱裝箱等 JDK 5+ 語法。

解決步驟(圖文)

  1. 右鍵項目 → Properties → Java Compiler。
  2. 取消勾選 Use compliance from execution environment。
  3. 將 Compiler compliance level 設置為 **1.6、1.7、1.8 或更高**(建議與運行時 JDK 一致)。
  4. 點擊 Apply and Close,等待項目重新構建,錯誤消失。

底層配置文件(.settings/org.eclipse.jdt.core.prefs)

若需批量修改或版本控制共享,直接編輯該文件:

eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.source=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
# 其餘保持默認...

關鍵三項:targetPlatform、compliance、source 必須一致,且 ≥ 1.5。

Maven 項目額外檢查

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
<!-- 或 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <encoding>UTF-8</encoding>
    </configuration>
</plugin>

執行 mvn eclipse:eclipse 或 IDE 重新導入 Maven 項目,同步編譯器設置。

總結

「enum 識別符錯誤」99% 是編譯器合規級別過低。一鍵調整至 1.8+,問題秒解。建議團隊統一 .settings 文件納入版本控制,避免「我機器上好使」的環境差異。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved