在 Eclipse/MyEclipse 中使用 com.sun.image.codec.jpeg.* 等 JDK 內部 API 時,編輯器報錯:
Access restriction: The type '...' is not API (restriction on required library 'rt.jar')
預設編譯器將其視為 Error,導致專案無法編譯通過。
com.sun.*、sun.* 等套件屬於 JDK 內部實現 API,非標準公開 API將「禁止引用受限 API」從 Error 降級為 Warning,即可編譯通過(但保留警告提醒風險)。
Window > Preferences (Mac: Eclipse > Preferences)Java > Compiler > Errors/Warnings
若不想全域生效,可針對單一專案設定:
Properties > Java CompilerErrors/Warnings > Deprecated and restricted API| 內部 API (com.sun.*) | 標準替代方案 | 說明 |
|---|---|---|
| com.sun.image.codec.jpeg.* | javax.imageio.ImageIO | JDK 1.4+ 標準圖片 I/O API,支援 JPEG/PNG/GIF 等 |
| sun.misc.BASE64Encoder/Decoder | java.util.Base64 (JDK 8+) | 標準 Base64 編解碼,效能更好、API 更清晰 |
| com.sun.net.httpserver.* | Spring Boot / Jetty / Undertow | 生產環境建議使用成熟 Web 框架 |
@SuppressWarnings("restriction") 並添加註釋說明原因--add-exports 才能存取,更應加速遷移提醒:降級為 Warning 僅是暫時規避編譯錯誤,生產環境強烈建議替換為標準 API,確保長期維護性與跨 JDK 版本相容性。