How to solve IllegalAccessException

Because of Cloud Security restrictions, certain actions from Java are blocked. When Emulate Cloud Security is turned off, I don't get the errors below, but when I turn it on, I get the following: java.lang.IllegalAccessException: Method [public java.lang.String org.thymeleaf.expression.Calendars.format(java.util.Calendar,java.lang.String)] cannot be accessed. java.lang.IllegalAccessException: Method [public java.lang.String java.util.Locale.getCountry()] cannot be accessed. (I am using thymeleaf-2.0.18.jar) I want to run the app in the Mendix cloud as well, so I must change the policy file to get it working. What should I add to the policy file (security.policy) to avoid the IllegalAccessExceptions?
2 answers

This doesn't look like it has anything to do with policy files, you would usually get an AccesControlException (or something like that) if that were the case. From the Java doc:

An IllegalAccessException is thrown when an application tries to reflectively create an instance (other than an array), set or get a field, or invoke a method, but the currently executing method does not have access to the definition of the specified class, field, method or constructor.

In other words: you (or the library) is trying to access a method that is (probably) marked "private". You'll have to work around this in your code. See for example:

But note that the permission mentioned there (java.lang.reflect.ReflectPermission suppressAccessChecks) doesn't look like something we're going to include in our policy file.


I seems there is a problem in Thymeleaf/OGNL. See also this link.

A solution is to call OgnlRuntime.setSecurityManager(null); first before using Thymeleaf.