Using JavaCompiler with a Classpath referencing banks in the ear

I am working on a project in which a corporate archive (ear) deployed on a JBoss server must dynamically compile (and run) the class. I use the JavaCompiler class for this - the complexity comes from the fact that the compiled class has references to some classes contained in the jjb jar in the ear.

This is not a problem when the deployed ear is “exploded” during deployment, so it is just a directory, not an archive. In this case, I can specify the desired jar in the -classpath option of the compiler, and compilation works fine. Unfortunately, due to the limitations of the systems I work with, this is not an acceptable solution for deploying these ears, and the compiler seems to be unable to “see” the required jar when it ends in the archive.

Given that dynamic compilation comes from the ear, and therefore the system class loader has access to the contents of the required jar, is there any way I can tell that the compiler just uses the classes loaded by the system class loader?

I appreciate that this is something verbose, but any help would be appreciated.

thanks

+1
source share
1 answer

There seems to be no easy way to load JavaCompiler load dependencies of compiled code with ClassLoader . However, you can implement the JavaFileManager directly and redirect operations for StandardLocation.CLASS_PATH by searching for resources in the context of ClassLoader ( getResource(<class/resource name>) ). This will remove the restriction of StandardJavaFileManager directly working on File s.

Someone already seems to have prototypically fulfilled this statement: http://atamur.blogspot.de/2009/10/using-built-in-javacompiler-with-custom.html

+1
source

Source: https://habr.com/ru/post/1484089/


All Articles