使用 Ant 时出现 java.security.AccessControlException,但从控制台调用 java 时运行正常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12195868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.security.AccessControlException when using Ant, but runs ok when invoking java from console
提问by Marcus Junius Brutus
I have the following Ant < java >task:
我有以下Ant < java >任务:
<property name="classpath-run.msg" refid="run.classpath"/>
<echo message="running the app with classpath = ${classpath-run.msg}"/>
<echo message="Java version used (ant.java.version): ${ant.java.version}"/>
<echo message="Java version used ( java.version): ${java.version}"/>
<java classname ="${project.MainClass.name}">
<permissions>
<grant class="java.security.AllPermission"/>
<grant class="javax.management.MBeanTrustPermission" action="register"/>
</permissions>
<classpath refid="run.classpath"/>
</java>
When I run Antthe execution of the task fails (see trace below). When I copy-paste the echoed classpath and run my code from the command line with java -classpath ..the execution succeeds!
当我运行蚂蚁的执行任务失败(见下面的跟踪)。当我复制粘贴回显的类路径并使用java -classpath从命令行运行我的代码时,执行成功!
In both cases the same version of javais used.
在这两种情况下,都使用相同版本的java。
How is it possible that I observe different behavior ? A java.security.AccessControlExceptionsits at the root of the stacktrace but I've already granted my Ant javatask all security permissions, including, explicitly the MBeanTrustPermissionfor the action registerwhich also appears in the stack trace.
我怎么可能观察到不同的行为?一个java.security.AccessControlException坐在堆栈跟踪的根,但我已经给予我的Ant的java任务中的所有安全权限,包括明确的MBeanTrustPermission的行动寄存器也出现在堆栈跟踪。
Trace (when run under Ant) is found below:
跟踪(在Ant下运行时)在下面找到:
[java] java.lang.ExceptionInInitializerError
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:194)
[java] at org.apache.tools.ant.taskdefs.Java.run(Java.java:771)
[java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:221)
[java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:135)
[java] at org.apache.tools.ant.taskdefs.Java.execute(Java.java:108)
[java] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
[java] at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[java] at java.lang.reflect.Method.invoke(Method.java:601)
[java] at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
[java] at org.apache.tools.ant.Task.perform(Task.java:348)
[java] at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
[java] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
[java] at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[java] at java.lang.reflect.Method.invoke(Method.java:601)
[java] at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
[java] at org.apache.tools.ant.Task.perform(Task.java:348)
[java] at org.apache.tools.ant.taskdefs.MacroInstance.execute(MacroInstance.java:398)
[java] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
[java] at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[java] at java.lang.reflect.Method.invoke(Method.java:601)
[java] at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
[java] at org.apache.tools.ant.Task.perform(Task.java:348)
[java] at org.apache.tools.ant.Target.execute(Target.java:390)
[java] at org.apache.tools.ant.Target.performTasks(Target.java:411)
[java] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
[java] at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
[java] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
[java] at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
[java] at org.apache.tools.ant.Main.runBuild(Main.java:809)
[java] at org.apache.tools.ant.Main.startAnt(Main.java:217)
[java] at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
[java] at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
[java] Caused by: java.lang.ExceptionInInitializerError
[java] at TranslationClient.main(TranslationClient.java:5)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[java] at java.lang.reflect.Method.invoke(Method.java:601)
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.run(ExecuteJava.java:217)
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:152)
[java] ... 34 more
[java] Caused by: net.sf.ehcache.CacheException: java.security.AccessControlException: access denied ("javax.management.MBeanTrustPermission" "register")
[java] at net.sf.ehcache.management.sampled.SampledMBeanRegistrationProvider.initialize(SampledMBeanRegistrationProvider.java:98)
[java] at net.sf.ehcache.management.provider.MBeanRegistrationProviderImpl.initialize(MBeanRegistrationProviderImpl.java:63)
[java] at net.sf.ehcache.CacheManager.doInit(CacheManager.java:454)
[java] at net.sf.ehcache.CacheManager.init(CacheManager.java:374)
[java] at net.sf.ehcache.CacheManager.<init>(CacheManager.java:356)
[java] at TranslationCache.<clinit>(TranslationCache.java:10)
[java] ... 41 more
[java] Caused by: java.security.AccessControlException: access denied ("javax.management.MBeanTrustPermission" "register")
[java] at java.security.AccessControlContext.checkPermission(AccessControlContext.java:366)
[java] at java.lang.SecurityManager.checkPermission(SecurityManager.java:585)
[java] at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.checkMBeanTrustPermission(DefaultMBeanServerInterceptor.java:1848)
[java] at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:322)
[java] at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:512)
[java] at net.sf.ehcache.management.sampled.SampledMBeanRegistrationProvider.registerCacheManagerMBean(SampledMBeanRegistrationProvider.java:118)
[java] at net.sf.ehcache.management.sampled.SampledMBeanRegistrationProvider.initialize(SampledMBeanRegistrationProvider.java:95)
[java] ... 46 more
回答by Friederike
I had a similar problem. Surprisingly for me a fork="yes"
in the java task worked like a charm. I didn't have to change any security policies.
我有一个类似的问题。令我惊讶的是fork="yes"
,java 任务中的一项工作就像一个魅力。我不必更改任何安全策略。
I would try this:
我会试试这个:
<java fork="yes" classname ="${project.MainClass.name}">
<classpath refid="run.classpath"/>
</java>
回答by tbrugz
I had the same problem when trying to register an MBeanwith ant, and the solution was to add to %JAVA_HOME%\jre\lib\security\java.policy
the following line:
我在尝试使用ant注册MBean时遇到了同样的问题,解决方案是添加到以下行:%JAVA_HOME%\jre\lib\security\java.policy
permission javax.management.MBeanTrustPermission "register";
权限 javax.management.MBeanTrustPermission "注册";
reference: message from ant-users
参考:来自蚂蚁用户的消息
回答by Marko Topolnik
Apparently, Ant installs a SecurityManager
when running your program and the way your JAAS security is configured, the code doesn't have the necessary privileges to run correctly.
显然,AntSecurityManager
在运行您的程序时安装了一个JAAS 安全配置方式,代码没有正确运行所需的权限。
By default no SecurityManager
is installed, therefore your code runs normally when executed directly from the command line.
默认情况下没有SecurityManager
安装,因此当直接从命令行执行时,您的代码运行正常。
You can consult the documentation for the Java Taskand look under "permissions".