Java Magazine, Jan/Feb 2016
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2016 62 path to java 9 Eagle eyed developers will note that this means that module is a new keyword in Java 9 For this to compile the directory structure needs to be laid out according to the new modular source rules For our module com jdk9ex this structure is the following ____ src ____ com jdk9ex ____ com ____ jdk9ex ____ Main java ____ module info java Note that the module name is independent of the normal conventions for Java packages that live inside the module To compile this modular code we need to tell javac that were running a module aware build javac d modules com jdk9ex src com jdk9ex module info java src com jdk9ex com jdk9ex This produces a set of modular class files but not a jimage To execute this code we also need to explicitly inform the JVM that modules are needed java modulepath modules m com jdk9ex com jdk9ex Main Hello Modules The basic Hello World example is an established pattern for programmers and experimenting with the modules system and seeing how it works is highly recommended for developers who want to fully understand the current state of modularity for Java 9 What could go wrong Inevitably some aspects of any new technology will cause problems for early adopters so lets look at a couple of basic failure cases In particular if you fail to build or invoke modular Java correctly you might see a new sort of exception java modulepath modules m com jdk9ex Main Error occurred during initialization of VM java lang module ResolutionException Module com jdk9ex Main not found at java lang module Resolver fail java base@ 90 Resolver java 880 at java lang module Resolver resolve java base@ 90 Resolver java 193 at java lang module Resolver resolve java base@ 90 Resolver java 173 at java lang module Configuration resolve java base@ 90 Configuration java 229 at jdk internal module ModuleBootstrap boot java base@ 90 ModuleBootstrap java 174 at java lang System initPhase2 java base@ 90 System java 1242 Some lines have been wrapped to fit Ed This single stack trace shows that we have the following new items Packages including java lang module Exceptions including ResolutionException Locations for code such as java base@ 90 ModuleBootstrap java Entry points for startup such as java lang System initPhase2 From this we can also see that the new modular paths used for jimages are now directly present in stack traces Alternatively we might try to access an internal implementation class The code shown below compiles and runs on Java 8 import sun invoke util BytecodeName public class InternalsCheck
You must have JavaScript enabled to view digital editions.