9

I have the following Ant < java > task:

<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 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!

In both cases the same version of java is used.

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.

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
Marcus Junius Brutus
  • 26,087
  • 41
  • 189
  • 331

3 Answers3

17

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.

I would try this:

<java fork="yes" classname ="${project.MainClass.name}">
    <classpath refid="run.classpath"/>
</java>
Friederike
  • 1,252
  • 15
  • 29
10

I had the same problem when trying to register an MBean with ant, and the solution was to add to %JAVA_HOME%\jre\lib\security\java.policy the following line:

permission javax.management.MBeanTrustPermission "register";

reference: message from ant-users

tbrugz
  • 470
  • 1
  • 7
  • 17
3

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.

By default no SecurityManager is installed, therefore your code runs normally when executed directly from the command line.

You can consult the documentation for the Java Task and look under "permissions".

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • that looks like a plausible explanation. I'll investigate it and come back. – Marcus Junius Brutus Aug 30 '12 at 11:44
  • Added permissions (see updated Ant java task) but still fails with the same stack trace. – Marcus Junius Brutus Aug 30 '12 at 12:28
  • Actually, I copied that use for the java.security.AllPermission class from here: http://ant.apache.org/manual/Types/permissions.html. The use for the MBeanTrustPermission I copied from here: http://docs.oracle.com/javase/7/docs/api/javax/management/MBeanTrustPermission.html#MBeanTrustPermission(java.lang.String,%20java.lang.String) – Marcus Junius Brutus Aug 30 '12 at 12:57