在 Eclipse 中使用 com.sun.image.codec.jpeg.JPEGCodec 和 JPEGImageEncoder 處理圖片時,編輯器報錯:
Access restriction: The type JPEGImageEncoder is not accessible due to restriction on required library C:\Java\jre1.6.0_07\lib\rt.jar
com.sun.* 包屬於 Sun/Oracle 內部實現 API,非標準公共 API。Eclipse 默認將此類受訪問限制的 API 設置為 ERROR 級別,阻止編譯通過。
javax.imageio.ImageIO 配合 ImageWriter 進行 JPEG 編碼,避免依賴內部實現。@SuppressWarnings("restriction") 精確抑制警告,而非全局降級。--add-exports 顯式開放,遷移成本更高。// 推薦的標準寫法
BufferedImage image = ...;
ImageIO.write(image, "jpg", new File("output.jpg"));