在Java中获取SecurityException

在Java中获取SecurityException,java,Java,为什么我会犯这个错误 Exception in thread "main" java.lang.SecurityException: Unsafe 当我运行代码时 public class UnsafeString { public static void main(String[] args) throws InstantiationException { String obj = new UnsafeString().create(); System

为什么我会犯这个错误

Exception in thread "main" java.lang.SecurityException: Unsafe
当我运行代码时

public class UnsafeString {
    public static void main(String[] args) throws InstantiationException {
        String obj = new UnsafeString().create();
        System.out.println(obj);
    }
    public String create() throws InstantiationException {
        return (String) sun.misc.Unsafe.getUnsafe().allocateInstance(String.class);
    }
}

您不能直接使用
不安全的
。它涉及一些黑客来避免
SecurityException
(主要是通过反射获取
不安全的
实例)。 假设您没有做一些愚蠢的事情,比如在生产代码中使用它,这是一种检索它的方法


还有传言说,它以后会成为公共API的一部分。

你为什么要写这些代码?强烈建议程序员不要使用
sun.*
类。