将JavaCompiler与Classpath引用耳朵内的jar [英] Using JavaCompiler with Classpath referencing jars within ear

查看:329
本文介绍了将JavaCompiler与Classpath引用耳朵内的jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,部署在JBoss服务器上的企业归档文件(ear)需要动态编译(并运行)一个类.我正在使用JavaCompiler类来执行此操作-复杂性来自以下事实:正在编译的类引用了耳内ejb jar中包含的某些类.

当部署的耳朵在部署时爆炸"时,这不是问题,因此它只是目录而不是存档-在这种情况下,我可以在编译器的-classpath选项中指定所需的jar,和编译工作正常.不幸的是,由于我正在使用的系统的限制,将这些耳朵爆炸"部署是不可接受的解决方案,并且编译器似乎无法看到"所需的jar,将其包装在存档中./p>

鉴于动态编译是从有问题的耳朵进行的,因此系统的类加载器可以访问所需jar的内容,有什么办法可以告诉编译器仅使用由加载的类?系统类加载器?

我很高兴这是一个冗长的问题,但是任何帮助都将不胜感激.

谢谢

解决方案

似乎没有简单的方法来使JavaCompilerClassLoader加载已编译代码的依赖项.但是,可以直接实现JavaFileManager并使用上下文ClassLoader(getResource(<class/resource name>))上的资源查找重定向StandardLocation.CLASS_PATH的操作.这样可以取消直接在File上运行的StandardJavaFileManager的限制.

某人似乎已经实现了该方法的原型: http://atamur.blogspot.de/2009/10/使用内置于javacompiler-with-custom.html

I am working on a project in which an enterprise archive (ear) deployed on a JBoss server needs to compile (and run) a class dynamically. I am using the JavaCompiler class to do this - the complication comes from the fact that the class being compiled has references to some of the classes contained within the ejb jar within the ear.

This is not a problem when the deployed ear is 'exploded' on deployment, so it is just a directory rather than an archive - in this case I am able to specify the required jar in the -classpath option of the compiler, and compilation works fine. Unfortunately due to constraints of the systems I am working with, it is not an acceptable solution to deploy these ears 'exploded', and the compiler seems not to be able to 'see' the required jar when it's wrapped up in an archive.

Given that the dynamic compilation is taking place from the ear in question, and therefore the system's class loader has access to the contents of the required jar, is there any way I can tell the compiler to just use the classes as loaded by the system class loader?

I appreciate this is something of a wordy question, but any help would be appreciated.

Thanks

解决方案

It seems that there is no simple way to have the JavaCompiler load dependencies of compiled code from a ClassLoader. However, one could implement JavaFileManager directly and redirect the operations for the StandardLocation.CLASS_PATH using resource lookups on the context ClassLoader (getResource(<class/resource name>)). This would withdraw the limitation of StandardJavaFileManager directly operating on Files.

Someone already seems to have prototypically implemented that approch: http://atamur.blogspot.de/2009/10/using-built-in-javacompiler-with-custom.html

这篇关于将JavaCompiler与Classpath引用耳朵内的jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆