is ClassLoader.loadClass() supposed to work on module-info classes?

Alex Buckley alex.buckley at oracle.com
Thu Dec 3 19:46:20 UTC 2015


On 12/3/2015 10:04 AM, Peter Levart wrote:
> Loosely related to this debate, I would like to ask what's the story
> with modular jars that can run on JDK8 classpath and as modules on JDK9.
> How is one supposed to create such jar file as it must have:
>
> - classes compiled with JDK8 javac (or JDK9 javac with -source 8 -target
> 8 or -release 8)
> - module-info.class which can only be produced with JDK9 javac, but not
> with -source 8 -target 8 or -release 8
>
> Is one expected to invoke javac twice, 1st for sources (excluding
> module-info.java), 2nd just for module-info.java ?

Yes (or get your build tool to do it).

More generally, anyone creating a multi-release JAR file (you didn't ask 
about them, and they are not necessary to achieve what you want above, 
but I mention them for completeness) will invoke javac multiple times. 
It's the cost of doing business.

> Wouldn't it be nice
> for jar tool to be able to fabricate the module-info.class from scratch
> using just command-line options?

No. How does a Java compiler know what's exported from a module, in 
order to enforce accessibility, if the module declaration isn't 
fabricated until packaging time?

I expect you will say, "Encode exports somewhere other than the module 
declaration, such as with @Exported annotations on types or packages." 
To which I repeat: "if we're going to introduce the concept of a
module to millions of Java developers, we see value in consolidating 
both kinds of configuration [dependencies and exports] in one place".

Alex


More information about the jigsaw-dev mailing list