java.lang.ExceptionInInitializerError Caused by: java.lang.SecurityException: Unsafe
阿新 • • 發佈:2021-02-11
技術標籤:異常javaUnsafe異常使用不了UnsafeSecutityExecep
問題描述:
突然奇想想試下unsafe類的方法,發現會報錯。
該行程式碼報錯
private static final Unsafe unsafe = Unsafe.getUnsafe();
java.lang.ExceptionInInitializerError
Caused by: java.lang.SecurityException: Unsafe
at sun.misc.Unsafe.getUnsafe(Unsafe.java:90)
at com.lbh.xxmanager.basic. java.thread.Ref.<clinit>(LockExample.java:62)
Exception in thread "main"
原因分析:
@CallerSensitive
public static Unsafe getUnsafe() {
Class var0 = Reflection.getCallerClass();
if (!VM.isSystemDomainLoader(var0.getClassLoader())) {
throw new SecurityException("Unsafe" );
} else {
return theUnsafe;
}
}
在使用該getUnsafe方法是,會判斷classLoader的型別,如果不是systemClassLoader則會丟擲SecurityException(“Unsafe”)異常,所以使用者編寫的程式使用不了unsafe例項。
解決方案:
放棄吧少年,此方案無解,如有少俠能解,還望指教。