gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:29:11 25 4
gpt4 key购买 nike

我有以下 Ant 任务:

<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 用于 register 操作,它也出现在堆栈跟踪中。

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

最佳答案

我遇到了类似的问题。令我惊讶的是,Java 任务中的 fork="yes" 工作起来就像一个魅力。我不必更改任何安全策略。

我会试试这个:

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

关于使用 Ant 时出现 java.security.AccessControlException,但从控制台调用 java 时运行正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12195868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com