我正在尝试 SecurityManager
类。我想检查当前线程是否有退出Java虚拟机的权限。下面是我想出的代码。
SecurityManager appsm = System.getSecurityManager();
System.out.println("something");
appsm.checkExit(0);
我原以为 SecurityManager.checkExit
会抛出一个 SecurityException
。但是,IDE 反而输出 NullPointerException
。
Exception in thread "main" java.lang.NullPointerException
at jtotheplatformenvironment.JTOThePlatformEnvironment.main(JTOThePlatformEnvironment.java:40)
C:\Users\Justin\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
为什么会这样?
最佳答案
您需要使用一个额外的参数来执行 Java 应用程序:
-Djava.security.manager
因此 JVM 将使用内置的默认安全管理器 (source) 启动,否则不会创建安全管理器,这就是您获得 NPE 的原因。
关于java - 如果当前线程有权限退出Java虚拟机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35713921/