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

Eclipse/MyEclipse 專案 Enum 關鍵字識別失敗快速修復指南

     編輯:Java 相關知識

現象

匯入舊專案後大面積紅錯,提示 Syntax error on token "enum", invalid Expression,編譯器不識別 enum 關鍵字。

根因

專案 Java Compiler Compliance Level 設定過低(≤ 1.4)。enum 為 Java 5 引入,需 compliance ≥ 1.5。

兩種修復方案(擇一)

方案一:IDE 介面調整(推薦)

  1. 專案右鍵 → Properties → Java Compiler
  2. 取消勾選 Use compliance from execution environment
  3. Compiler compliance level 選 1.5 或更高(如 1.8、11、17)
  4. Apply → Yes(重新建置)

方案二:直接修改組態檔

編輯專案根目錄 .settings/org.eclipse.jdt.core.prefs,確保關鍵屬性 ≥ 1.5:

eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.source=1.8
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error

儲存後專案右鍵 → Maven → Update Project 或手動 Clean/Build。

驗證

新建測試類:

public enum Color { RED, GREEN, BLUE }

無紅錯即修復成功。

避坑提醒

  • Execution Environment (JRE) 需 ≥ 設定的 compliance 版本
  • Maven 專案同步 maven-compiler-plugin source/target 版本
  • 團隊統一 .settings 納入版控,避免環境不一致

一分鐘搞定 Enum 識別失效,回歸高效開發。

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