使用Ant时java.security.AccessControlException,但从控制台调用java时运行正常 [英] java.security.AccessControlException when using Ant, but runs ok when invoking java from console

查看:137
本文介绍了使用Ant时java.security.AccessControlException,但从控制台调用java时运行正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 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 .. 从命令行运行我的代码时,执行成功!

When I run Ant the 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

In both cases the same version of java is used.

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

How is it possible that I observe different behavior ? A java.security.AccessControlException sits at the root of the stacktrace but I've already granted my Ant java task all security permissions, including, explicitly the MBeanTrustPermission for the action register which also appears in the stack trace.

跟踪(在 Ant 下运行时) )发现如下:

Trace (when run under Ant) is found below:

 [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


推荐答案

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

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.

我会尝试这样做:

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

这篇关于使用Ant时java.security.AccessControlException,但从控制台调用java时运行正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆