Use a custom classloader at compile time

試著忘記壹切 提交于 2019-12-01 06:50:24

When you run javac you can specify the classloader like so:

javac -J-Djava.system.class.loader=org.awesome.classloader sourcefile.java

It may be possible to initialize a custom classloader and then use it while calling the new Java 6 Compiler API in javax.tools.

The only two ways I know of plugging directly into javac (as run on the command line) are via the annotation processor or via a compiler-specific hack.

Just to expand on Michael's answer, if you cannot use Java6, look at the sun. packages--they have always been available to java apps and have always had a compiler in there, it's just not standard Java so you don't hear about it much and the API is subject to change (like moving it to the javax.tools package!)

Take a look at ClassLoader.defineClass. I used it myself for loading plugins into a program I created, in which I loaded a file's bytes into a new class.

If the classes all conform to the same Interface you could just provide that at compile time..

If not then I don't see what you are gaining by not outputing .java files based on the DB and compiling that.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!