Hello there. Today we are going to look at how to resolve java.lang.reflect.InvocationTargetException error that occurs when trying to open groovy shell or groovyConsole or any other groovy commands.
This error usually occurs when you have newly installed Groovy and when trying to execute any groovy commands. This error is occurring because of some permission issues with the ‘tmp’ folder and can be easily resolved using a simple command from the SDKMan.
Error:
^[[D^[[DWARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass (file:/usr/share/groovy/lib/groovy-2.4.21.jar) to method java.lang.Object.finalize()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:116)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:138)
Caused by: java.lang.UnsatisfiedLinkError: Can't load library: /usr/lib/jvm/java-11-openjdk-amd64/lib/libawt_xawt.so
at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2638)
at java.base/java.lang.Runtime.load0(Runtime.java:768)
at java.base/java.lang.System.load(System.java:1850)
Solution:
The solution to this is switching the Groovy version using the SDKMan to 2.4.17 as this version is found to be working properly.
If you havent installed SDKMan, you can follow this article on Installing SDKMan in Ubuntu to have it installed before trying out this solution.
Execute the following command to list all the Groovy versions:
sdk list groovy
It will list all the installed Groovy versions as shown like the below screenshot:
To install specific Groovy version and switch to that version, execute the following command:
sdk install groovy 2.4.17
This will install the Groovy 2.4.17 and will set it as the default version. You can follow our article on Installing Java OpenJDK in Ubuntu to install the Java as it is also required for Groovy to function.