Use custom class loader at compile time

Is it possible to specify a custom classloader for javac (or some alternative java compiler)?

I would love such a feat because it would allow me to compile classes that use classes that are only found by my special class loader.

For the curious: I would write a classloder that connects to the database and creates classes based on the found tables.

+4
source share
6 answers

When starting javac, you can specify the class loader as follows:

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

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

+4
source

The only thing I know about connecting directly to javac (as executed on the command line) is the annotation handler or through a compiler-specific hack .

+1
source

Just to expand Michael's answer, if you cannot use Java6, look at the sun. packages - they were always available for java applications and always contained a compiler there, it’s just not standard Java, so you don’t hear too much about it, and the API can change (for example, move it to the javax.tools package!)

0
source

Take a look at ClassLoader.defineClass . I myself used it to load plugins into a program I created in which I uploaded a byte file to a new class.

0
source

If all classes correspond to the same interface, you can simply specify that at compile time.

If not, then I don’t see what you get without outputting .java database-based files and compiling it.

0
source

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


All Articles