AdSense

網頁

2019/9/26

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]即可。



參考:

沒有留言:

AdSense