>> Greetings everyone and welcome back to
Microsoft Java for beginner series.
My name is Brian Benz and I'm a Cloud Advocate at Microsoft.
I'm joined by Kirk Pepperdine who's going to tell
us a little bit about Java modules.
Kirk, do you want to describe
modules and introduce yourself as well.
>> Hi Brian. I am
a Principal engineer in the Java Engineering Group,
and so we're responsible for delivering buildup OpenJDK.
I'm here to talk about modules introducing JDK 9.
They really allow you to
separate the public bits of a library from
the private bits of the library so that people don't build
dependencies on the things that you might want to change.
Primarily focused initially on a JDK,
but it's generally available for 100 years and we'll dig in.
The Java Platform Module System is a mechanism by which we
can collect artifacts that are related and put them into a bundle.
We can explicitly indicate
which parts of this bundle are visible for
public use and which parts of the bundle are
concealed and the public doesn't have direct access to them.
We end up with really an extension of
the visibility or accessibility models that were defined in
JDK1 and have remained with us to JDK 8.
Now, there's still with us,
so we can still see that we have private package and
protected and public visibility
to many of the artifacts within well,
totally all the artifacts within Java.
But when we look to modules,
what we're saying is that we can make things public to
friends or we can make things public to everybody.
More importantly, we can make also things private so
that people don't accidentally build
dependencies upon things that we may need to change.
The JDK libraries were modulized for the 9.0 Release.
As you can see, there was a lot of work
here trying to separate things out
and tried to get rid
of any of this circular dependencies that may have existed.
If you look at this diagram very closely,
you're going to see there's a lot of
dependency mappings between things,
but none of them are circular and everything is going to
rely eventually on this thing would known as the Java base.
Now if you look closer into Java base,
you can see that what they've done is they've exposed for
public consumption things like java.lang,
java.io, and so on.
What they've kept private are the sun internal classes
like sun.nio.ch, or sun.reflect.annotation.
These classes were never intended for public consumption,
but sometimes people use them anyways for whatever reason.
Having them in modules means that now
the JDK developers can do their job without interfering
with anybody's application because the application
simply don't have access to these internal classes.
Let's use this small example,
Hello to explore some of the features of modules.
We'll start by looking at the code.
Here I have declared a method which is going
to take in a string and concatenate Hello to it,
and then return that string to the color.
Here in our main method,
I'm going to use all of this to say hello to Fred.
The first thing we need to do to turn this into
a module is provide a module descriptor.
So I'm just going to create a new file.
A module descriptor goes into a file known as module-info.java,
and that has to be in the base package.
Declare our module, give it a name, com.Microsoft.hello.
We need to say everything that we're dependent upon.
This is a requires clause in here.
The only thing we're really dependent upon is java.base.
But since that's provided with the JDK,
it's implied that we have this width is meaning that we don't have
to explicitly state this requirement or this dependency.
We're done. We've created our first module.
Now, all we need to do is
compile it and package it in, then we can use it.
To do that, I'm going to first compile it Javac -d classes.
We're going to specify now
the classes that we want to be compiled.
There we go and do it again.
Hello, mean Java modules.
Next, we're going to jar and create a file,
which is going to be in the lib directory and we're going
to call it hello.jar.
Here I'm going to specify a main class com.Microsoft.
You don't have to do this,
but it makes it easier to use for the first case, for using it in.
Now we need the rest of the bits
to say where the classes actually are and we're good.
Now I can run this in
the traditional way because it's just a jar file.
Here's our expected result.
I can also run it as a module.
So I'm going to create the module path right here.
I say which module I'm interested in running,
which is now com.Microsoft.hello.
If I hadn't set up the main class and
the manifest using the jar command,
then I would have had to add extra information here to
say which class I wanted to execute out of this module.
There's our expected output.
Now, if that's all there was to it,
this wouldn't be very interesting.
What we'd like to do now is take this wonderful library that we've
written and let the world be able to take advantage of it.
Let's take that step by creating another project
and we'll have that other project consume this module.
I'm going to do that here. I'm going to call this greeter.
The first thing I want to do is just clean this out.
These bits lying around when we work with greeter.
You can see here we have greeter.
It's much simpler because we're
going to use our wonderful library.
All I've had to do is create Hello object.
Now I can send a message of hello to Anna.
I need to turn this into a module.
I'm going to do the same thing as I did before. Create File.
Here's our module descriptor in
the info file and name it as before, com.Microsoft.greeter.
Here I need to put in requires because I do require that module.
I'm going to put that dependency explicitly rate in here.
Now we've written our second module.
We just need to compile it and package it,
and we should be able to run it.
Let's go off and try doing that.
First thing I'm going to do a Javac -d classes.
Now I need to put in a module path here.
I need to do that so
the compiler can see the definition for hello.jar.
Next, I am going to put in
just the all the classes, Microsoft greeter,
greeter.java and then we have
greeter source main Java module info.
We get an error. We get an error because
com.Microsoft.hello is not a package, it's visible.
That's because we haven't exported it from the hello module.
If we haven't exported anything from the hello module,
then nothing will be visible inside that module.
We need to go back and correct that problem first.
Let's go into module-info.
I'm going to say this exports com.Microsoft.hello.
We're exporting that particular package.
Good. Now I'm going to go back over to my window here.
I'm going to say, okay,
now we need to recompile and
rebuild that library. Let's open that up.
Javac, such as c classes, that's okay.
Here's our hello. That should compile everything.
Now we need the jar.
There's a jar. I'm not
going to specify the main class this so I'm going to
remove that class is gone.
Now we've recreated the jar file.
I can go back and Java C, the greeter.
Everything is looking good here.
You can see this time the compile work,
so the runtime will complain
if you try to access something in
the runtime that you haven't exported.
The compiler will also complain if you try to
compile against something that you haven't exported.
Next one I'm going to do is I'm going to package it.
I'm going to take a jar,
I'm going to create a file,
which we're going to call a greeter.
The lip directory, greeter.jar.
I'm going to this time declare a main class,
com.Microsoft.greeter.
Put in the rest of the bits that we need.
There's one thing I'd made a mistake in here,
and let's just make sure that we clean that out.
We don't want helloing here.
We need to get rid of that.
That's our classes and dot.
Again, I can do a module path with lib and specify the module.
This time it's going to be com.Microsoft.greeter.
When we run that, we can see that there's
now sending the message to Anna.
>> Thanks, Kirk. That was great.
>> Please check out the rest of the Java for beginner series.
>> Thank you, Brian.