茫茫網海中的冷日 - 對這文章發表回應
茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1729591 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00102.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2021/8/2 7:59:56

Eclipse Access restriction: The type '[ClassName]' is not API (restriction on required library '/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/rt.jar')

今天git pull同事的程式碼時,其中有幾個類別出現紅色叉叉錯誤,錯誤訊息如下:

Access restriction: The type '[ClassName]' is not API (restriction on required library '/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/rt.jar')


問題原因在於Eclipse對jre的access restrictions存取限制,也就是Eclipse預設會限制存取執行環境jre的非公開API(這些API僅限於jre本身使用)。由於同事在程式中用了一些jre的非公開API導致出現此錯誤。

解決方式有二,
一是取消Eclispe的access restrictions對所引用的jre非公開類別的限制;
二是把專案依賴的jre由執行環境(Execution environment)所使用的jre改為Alternate JRE。


取消Eclispe的access restrictions的限制方法為,在專案上 滑鼠右鍵 -> Build Path -> Configure Build Path... -> Java Build Path
打開Java Build Path設定,切換到[Libraray]標籤,展開JRE System Library,點選下面的 Access rules,然後按右邊的 Edit...打開Type Access Ruls編輯視窗。




然後在Type Access Ruls編輯視窗按 Add...新增規則。

在Add Access Rule視窗的[Resolution]欄位選擇[Accessible];[Rule Pattern]欄位加上要解除限制的類別package路徑,用 /
分隔,可以使用wildcard萬用字元,例如我要解除限制的類別在 sun.security的package下,所以設為 sun/security/**

設定完成後按 OK離開



然後在Type Access Rules視窗可看到剛新增的規則及所套用的package路徑。








或是直接把專案依賴的jre由執行環境(Execution environment)所使用的jre改為Alternate JRE。

在專案上 滑鼠右鍵 -> Build Path -> Configure Build Path... -> Java Build Path打開Java Build Path設定,切換到[Libraray]標籤,點選JRE System Library,按右邊的
Edit...

然後在開啟的JRE System Library視窗將使用的jre由[Execution environment]改為[Alternate JRE]即可。



參考:


原文出處:菜鳥工程師 肉豬: Eclipse Access restriction: The type '[ClassName]' is not API (restriction on required library '/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/rt.jar')*/
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|