使用Ant时java.security.AccessControlException,但从控制台调用java时运行正常

时间:2012-08-30 11:28:13

标签: java ant

我有以下 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>

当我运行 Ant 时,执行 任务失败(请参阅下面的跟踪)。当我复制粘贴回显的类路径并使用 java -classpath .. 从命令行运行我的代码时,执行成功!

在这两种情况下都使用相同版本的 java

我怎么可能观察到不同的行为? java.security.AccessControlException 位于堆栈跟踪的根目录但我已经授予我的 Ant java 任务所有安全权限,包括明确 MBeanTrustPermission < / strong>用于注册的操作,它也出现在堆栈跟踪中。

跟踪(在 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

3 个答案:

答案 0 :(得分:14)

我有类似的问题。令我惊讶的是,java任务中的fork="yes"就像一个魅力。我没有必要更改任何安全策略。

我会试试这个:

<java fork="yes" classname ="${project.MainClass.name}">
    <classpath refid="run.classpath"/>
</java>

答案 1 :(得分:8)

尝试使用 ant 注册 MBean 时遇到了同样的问题,解决方法是将%JAVA_HOME%\jre\lib\security\java.policy添加到以下行:

  

权限javax.management.MBeanTrustPermission“register”;

参考:message from ant-users

答案 2 :(得分:2)

显然,Ant在运行程序时安装SecurityManager以及配置JAAS安全性的方式,代码没有必要的权限才能正常运行。

默认情况下,没有安装SecurityManager,因此直接从命令行执行时代码运行正常。

您可以参考documentation for the Java Task并查看“权限”。