Skip to content
Advertisement

How to load a class from module(.jmod) file at runtime?

I want to load classes from a module (.jmod) file at runtime into the application memory.

I know that we can easily load classes from a (.jar) file using 🙂

JavaScript

The total code snippet can be found at this-repo in src omega.deassembler.JarLoader class

But the problem is URLClassLoader is unable to read the modules(.jmod).

So is there any other internal class or library to load classes from a module(.jmod) file.

Actually, I am creating an IDE and this-repo is a part of it for loading content-assist hints.

Earlier, I was using javap command to disassemble and load hints (see omegaide on github).

But this technique consumes much time, so i am writing it again!

Thank You.

Advertisement

Answer

This is not exactly the answer but a method to make URLClassLoader usable when it has to read modules.

Earlier URLClassLoader was throwing this exception when trying to read a module file(.jmod)

when omitting the module-info

JavaScript

and when including the meta-info

JavaScript

This means that it is unable to identify the directory system inside the jmod file.

So as we know, that a simple jar file contains classes and resources only (Just exclude meta-info).

And in a module file(.jmod), all the classes are placed inside the classes folder and all the resources are placed in the resources folder.

Thus, We can create a temporary jar file say “modular-jar.jar” with the contents of classes and resources from the module file,

and then using standard URLClassLoader we can load it onto the classpath

and then can immediately delete the file.

This will just work at least in my case

Here’s the code snippet

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement