Hello, the students in this lesson, we're going to learn more about features it was added in Java
version nine.
I'm talking about modules in Java.
There we have really a lot of new things to learn modules, really powerful features that exist in Java
platform.
And if you want to become Java software engineer or get one of Oracle certifications, you have to be
familiar with it.
We'll start lesson from simple and general topics such as what models are and just talking about why
we might need them to help you understand modules better.
I will explain what goals we are trying to achieve by using them and what benefits we expect to get.
We'll review different module types.
After that, we'll learn module directives that we use in the module descriptor and common line options
that might come in handy when you will work with multiple modules from Consult.
And at the end of the lesson, we're going to create modular Hello World application, and I will show
you how to configure it leaves and either you can work with this modular application from Eclipse.
As you can see, we have really a lot of things to do.
Let's start our lesson.
Let's understand first what modules are.
Module is a Nicklin named reusable group of related packages, as well as resources such as images and
Xolo files and module.
The specifying the modules name Xen Modules dependencies.
These are the other modules.
This module depends on zip packages.
It explicitly makes available to other modules.
All other packages in the module I implicitly unavailable to other modules, just services it offers,
the services it consumes, but what other modules it allows?
Reflection and General Module is a packaging mechanism that enables you to package a Java application
or Java API as a separate Java module and general module is packaged as Modular Jar File and Java module
can specify which of Java packages it contains.
That should be visible to OSR Java modules, which he uses.
This module and you have a module must also specify which OSI Jumo modules are required to do its job.
In most single words, Java module is a package for packages that is super simplified, but hope this
will help you to understand this concept.
You can group packages into modules and to ensure encapsulation, you may control access to different
packages within the module.
Modularity as higher level of a nation above packages is a clear.
Java Modules is a new feature in Java nine.
We as a Java Platform Module System Z Java Platform Module System is also sometimes referred to as Java,
Jigsaw or Project Jigsaw, depending on where you read.
Jigsaw was internally used project name during development.
Later, Jigsaw changed the name to Java Platform Module system.
There is a juicer 376 Juicer stands for Java specification request.
This is an actual description of proposed and final specification for the Java platform at any one time.
There are numerous juicers movies with a review and approval process.
In the Juicer 376, we can find list of goals of modular.
Ryzen is a Java Sea platform, and one them such goals as a reliable configuration.
Modularity provides mechanisms for explicitly declaring dependencies between modules in a manner that's
recognized both at compile time and execution time.
The system can work.
Suzy's Dependencies commands a subset of all modules required to support your app from Gemini nine and
forward Java applications must be packaged as Java Modules two zero four and application modules specifies
what OSM modules Java API modules supporting modules.
It uses zero for the Java virtual machine can check the whole module dependency graph from application
module and four one one zero virtual machine starts up.
If any required models are not found at startup, the Java Virtual Machine reports is a missing module
and shuts down before Java nine Mason class.
Just from a Mason Jar file, for example, would not be detected until the application actually tried
to use Zoom Mason class.
This would happen sometime at runtime dependent on one's application tried to use the missing class.
Having missing modules reported at application startup time is a big advantage compared to its runtime
when trying to use Zoom's Mojo Jar plus strong encapsulation zip packages in the module are accessible
to OSM modules only use the module explicitly exports them.
Even then, another module can't use those packages unless it explicitly states that it requires as
other modules capabilities.
This improves platform security because fewer classes accessible to potential attackers.
You may find that considering modularity helps you come up with cleaner, more logical designs.
Scalable Java Platform Robust is a Java platform was a monolith consisting of a massive number of packages,
making it challenging to develop, maintain and evolve.
It couldn't be easily subset.
The platform is now one of the rise into ninety five modules.
This number might change as Java evolves.
You can create custom rom coms consisting of only modules you need for your apps was devices you are
targeting.
For example, if a device doesn't support graphical user interfaces, you could create a runtime that
doesn't include xG.
UI modules significantly reduces your runtime size before Java nine and the Java Platform Module system.
You would have had to package all of the Java platform APIs with your job application because there
was no official way of reliable checking.
What crosses your Java application use?
Since the job platform API eyes have grown quite large over the years, your application will get the
large amount of Java classes included in its distribution, many of which your application would probably
not be using.
Greater platform integrity Before Java Non, it was possible to use many classes in the platform as
it were not meant for use by and apps.
Classes with strong encapsulation zones and channel API are truly encapsulated and hidden from apps
using the platform.
This can make migrating legacy code the Modularized Java non problematic.
If your code depends on internal APIs, then use glossies make up the key from distributable big, then
it needs to be.
This can be a problem on small devices like mobile phones, Raspberry Pi, etc. was a Java platform
modular system.
You can now package your application was always a module of the Java Platform API that your application
is actually using.
This will result in small application distribution.
Both improve performance.
Gem uses various optimization techniques to improve application performance.
GSR 376 indicates that these techniques are more effective when it's known in advance.
Word types are located only in specific modules, and this was a great improvement for Java as a platform.
Nowadays, questions about modules among many questions during different Oracle certifications.
Let's talk now about different modules types.
There are four types of modules in the new model system system modules.
These other modules listed when we run Java.
These modules Command Z, includes a Java Sea and JDK modules application modules.
These models are what we usually want to build when we decide to use modules z unnamed and defined as
a compile module info class file included in the assembled jar.
Today, we are going to create our own module info file and see all examples automatic modules.
We can include unofficial modules by an existing jar files to the module pass.
The name of the module will be derived from the name of the jar.
Automatic modules will have full access to every OSM module loaded by pass name tomorrow when a class
or jar is loaded into the class bus.
But no, the module bus.
It's automatically added to the unnamed model.
It's a catch all model to maintain backwards compatibility with previously written Java code.
The unnamed, more new concept is similar to the default package.
Therefore, it is not considered a real model but can be viewed as a default module.
We can create multi-modal projects comprised of a main application and several library modules.
We have to be careful, though, because we can only have one module per jar file.
Let's see now what module descriptor is and what are the different directives that might be used in
the module if a Java file, as we have discussed already, a module must provide a module descriptor
metadata that specifies the modules, dependencies, zip packages the model makes available to other
modules and more, and module descriptor is a compiled version of the module declaration that's defined
in the file name module.
Info jowar and the first scene that we need to do is to create a file was named Module Info and with
Java extension.
This file should be located in the source code directory.
Each module declaration begins with the keyword module, followed by a unique module name and the module
body enclosed in races like you see in that example.
In general, module name follows the same naming rules as Java packages.
However, you should not use underscores any module names or package names, class and these massive
names, variable names, etc. from Gemini and Forward because Java wants to use underscore as reserved
and easy to in the future.
It is recommended to name a Java module is the same as the name of the root Java package contained in
the module.
If that is possible, because some of this might contain multiple packages is a module declarations,
but it can be empty or may contain various module directives, including the requis experts provide
space, etc. We are going to discuss each of these in a few minutes.
As you'll see later, components and module declaration create is a model descriptor, which is stored
in the file named Module Info, plus the Modules folder.
On this slide, I will briefly introduce each module directive Zacchaeus only in module declarations
and may be used as identifiers anywhere else in your code.
Let's learn now directives.
The first directives that we are given to them is requires.
This module directive allows us to declare module dependencies, for example.
In this case, my module has both a runtime and the compile time dependency on some module that you
should refer by module name.
Each module must explicitly state its dependencies.
When Module eight requires Model B, Module A is set to read Module B and Module B is spread by Module
A, the specified dependency on another module user requires and all public Typekit.
Experts from a dependency are accessible by our module when we use this directive.
Another important mention here is that it is not allowed to have separate dependencies between modules.
In other words, if Module eight requires Module B, Module B can also require module a z module dependency
graph must be an acyclic graph and thus a directive is a quasi static.
We might write a lot of functions that pretty.
Our internal state one another to log in module is present, but not every consumer of our level will
want this functionality, and they don't want to include an extra log in library.
In this case, we want to use an optional dependency.
By using the request setting directive, we create a compile time only dependency.
Now let's learn transitive directive.
Imagine that we want to use third party library, and that library in turn has other dependencies.
How we can make sure that any module that we are dependent on will bring its own dependencies to work
properly.
We can force any downstream consumers also to read our required dependencies, which requires transitive.
Attractive, let's review exports DirecTV Now, as you already could understand, is a strong encapsulation
was one of the main goal when Java Platform Module System has been introduced by default, and Model
doesn't expose any of its API to other modules.
Our code is significantly more secure if we don't expose our API to external users.
But what if we created API that it should be used by different modules and we need to explicitly make
it available for everyone?
We use the experts directive to expose all public members of the named package.
Please note that only the least the package itself is experts now support packages of the experts package
experts.
That means that even this package, in my example, contains a package named You Zens Akombe.
My package name util package is not exported.
Just because of parent package is to export the SAP package.
Also, we must declare it explicitly in the module descriptor, and the one module is a singular package
can only be exported by a single Java module at one time.
In other words, you can't have two or more and more and use that export the same package in use at
the same time.
The Java Virtual Machine will come clean startup if you do having two modules export.
The same package is also sometimes referred to as a split package by split package is meant that the
total content classes of the package is split between the multiple modules.
This is not allowed.
In case we want to limit access to our API and expose it only to some packages XL, we have to use experts
to direct it similar to the experts directive.
We declare a package as experts, but we also list which models we are allowing to import this package
as it requires.
And here's how it looks like the next directive is users now use this Model Directive Force, a service
used by this module, making the model a service consumer, a service services, an object of loss that
implements the interface.
So extensive apps are specified in the users directive.
With this in mind, the services our model consumes with a users directive.
No, just the last name we use.
This is the interface for apps, loss of the service, the on the implementation class, because service
in this context means and implementation of a specific interface or apps a gloss that can be consumed
by OSI classes.
But what is the difference between uses and requires the wrappers we might require and more uses?
It provides a service we want to consume, but that service implements an interface from one of its
transitive dependencies instead of forcing our module to require all transitive dependencies.
Just in case we use the uses, Directive two adds the required interface to the module.
POS is a career.
We have discussed directives that consumes the services, but this also means that we have modules that
provides services so a module can also be a service providers that other modules can consume, and we
can ensure this voice provides with direction.
We should write provides key words first and specifies the interface or abstract class name of our service
is Let's see.
What we have to do is to specify what specific implementation of our service we are going to provide
was a help with directing.
You provide the implementation class name that isn't implements.
The interface or extents is an abstract class.
The next important declarations that I'd like to show you opens and opens to.
Let's learn this before December nine.
Reflection could be used to learn about all types in the package and all members of it time even its
private members, was the one that allows this capability on not.
Thus, nothing was truly encapsulated like we have discussed already.
The key motivation of the module system is strong, and cancellation by default and died in the module
is not accessible to OSM modules unless it's a public type and you export its package.
You expose all the packages you want to expose with Gemini.
This also applies to reflection.
For example, we can allow runtime only access to a package, so directive opened.
Some package indicates that a specific package is public types and then nested public and protected
types accessible to code in other modules at runtime only.
Also, all the types in the specified package and all of the types members are accessible via reflection.
Another example We can allow runtime only access the package by specific modules and opens to Model
Directive.
Like, for example, open package name to some list of modules indicates that specific packages, public
types and then nascent public and protected types are accessible to code in the list of modules at runtime
only.
All of the types in the specified package and all of the types members are accessible via reflection
to court in the specified modules.
We have discussed already that one of the main goals of modules is strong encapsulation, and I believe
that you watched the previous lesson and you're pretty familiar with reflection by this moment already.
So you can understand that before June nine, it was possible to use a reflection to examine every type
and the member in the package, even the private ones.
Was through.
Really encapsulated, which can open up all kinds of problems for developers of the libraries because
Java nine enforce a strong encapsulation.
We have to explicitly grant permission for OSM modules to reflect on our classes if we want to continue
to allow full reflection as older versions of Java did, we can simply opens entire modules by using
open direct before module name like you see on the slide.
Does it make sense so far?
And please always remember these in case you have any questions, please post them below this video
and I will be happy to answer those.
We have just learned directives that we can use in our modules in profile.
The next step would be to learn how to build our modules.
And before I show you examples of how to do that.
Let me explain.
Different command line options that we can use when we build Java modules, even despite the fact that
later in our course, we are going to learn such views as Randall and May one and zero when you have
support for building modules in Java.
It's still valuable to know how to use modules.
System forms a common plan will be used as a common line for our full example.
I'm going to show you in a few minutes to help solidify how the entire system works in our minds.
Module Pass.
We use a module pass option to specify the module for us.
This is a list of one on one directories that contain our modules athletes.
Instead of relying on the module declaration file, we can use the command line equivalent of the requires
directors and experts.
Common Line replacement for the X Plus DirecTV and Opens replaces the open clause in the module declaration
file adds modules adds is a list of modules into the default set of modules.
These modules creates a list of all modules and their version strings by each module.
Add or overwrite classes in modules.
Illegal acts was values is permit over or do not use a relaxed encapsulation by showing a single global
warning shows rewarding or fails with errors.
The default is permit.
Finally, we are done with the mini series piece.
Let's continue learning modules with examples now.
This would be our Hello World modular application.
Thus, it would all be super complicated because the main goal right now is to make you understand key
principles of modular application.
The rest of the synths will be super easy to understand if you learned how everything works on the hood.
So first of all, I want to say that for the sake of the demo, I created separate multi module project.
I will share with you a link to it in attachments to this lesson.
I have Food Project folder and I created the Modules folder.
Apparently, this folder will contain different modules of my app.
It is full of some main modules as a root source package.
For example, it was a top level package of the module is compatibles greetings.
Then I named the module in the same way.
Note this is a full stops.
These are modular root directory name.
Z's full stops need to be easier because part of the module name Zen not to be interpreted as subdirectory
POS dividers.
Is it the.
As you can see, I have two models here.
Let me start from greeting Swan.
It contains loss was named greetings and static mass insight that brings hello world to console.
Each model should have Model E for file.
Let's look at the model descriptor of this model.
On the top, you can see model name.
Basically, model name and name of the model for this should match and be expert package from this model.
Experts directive is used was package name.
Now creates a second model is the name of my second module is Compatibles App.
This is the main module that uses greetings module.
It has only one class was the main message where we use greetings class to invoke say hello mass pay
attention.
We also have the import statement here to import greetings class from another package.
This module also has module descriptor.
Let's look at it.
Here we can see that this module requires my greetings module, and this is all of this agree because
we are using class from greetings module in this one.
Then the stand dependencies between these two modules, if he isn't great, if no, please press pause
for a minute and think about it one more time.
The next step that we need to do this to compile our models specially for that, I create a script that
will compile all sources.
It is located in the root directory and it is called Build this sage I use Linux shall come here, so
pay attention to your transit script, either on Linux platforms or from Bosch on your Windows machine
and do not use Windows Common Line to run this script.
You know what Java commands?
This is our Java compiler.
After that, I pass the name of the output directly to specify where I want to put the compiled classes.
The next thing I do, I need to pass module source plus option.
This is different from the one I shared before.
If you remember the interview module courses on a separate slot, there is a difference between module
costs and module source boss.
The component needs a distinction between the code it used to compile in module source, boss and location
of compiled dependencies and libraries.
Module boss does it make more sense now?
I was here name of my folder with modules.
After that, I used Show Command to find old files with Java Extension in Modules folder.
Once my modular app is compiled, I can run it.
I have another script for this.
Is it a school run app or sage?
Let's look at it in this script.
You also see program that is familiar to you, Java.
I am going to start Java Virtual Machine.
I use option module POS to puzzle the reference to the compiled modules and after completion, all of
them are stored in target output directory.
You can see that all files here have class extension and even module info file is compiled into class
file.
Zen I specifies that most of the class it contains main message zip past consists of module name and
full class name.
So in this particular example, Compatibles Rules app is the model name and call it Bus App.
App is a full class name.
Is this clear?
For the sake of the demo, I opened it March inside eclipse.
I run build S.H. script first, and after that I run run apps, script and hindcast so we can see Hello
World from the application.
Can you see this?
Congratulations to you with your first modular application.
One more small comment here was the moment of recording this video.
There is no single and proper way of using Eclipse to run multi-modal application.
There might be some additional steps to configure multimodal application in eclipse.
That's why, even on your work, most likely you would use common client to build and to run your complex
enterprise application for the sake of convenience.
You still can work in eclipse, but do you need to be prepared that sometimes you will see more errors?
Is unusual because sometimes it is hard to conceive of all dependencies, not in the app, but in the
creep's itself in order Eclipse would understand the clearly dependencies between all modules.
Here's an example you can import two modules as separate projects and work in Java editor was Java files.
But in app module, you may see error because Eclipse can't see dependency on another module.
Since technically fruit loops, this is a separate project, and the groups allows only one module info
file per project.
In order to remove the error.
Sometimes you may need to configure dependencies manually.
If Eclipse didn't manage to configure everything automatically, for example, in my app module, I
open built up submenu, configure builds bus and here on library staff.
On the Module POS, I select Add Class Folder.
Here I put checkbox next to the modules that they need in this one, namely Reding's module and press,
OK.
Now you wouldn't see any errors in the clips, but in case you have multiple modules and not all of
them, I configure it automatically.
This might take time during the configuration of your project very first time after these configuration
steps.
You may run even from Eclipse.
Can you see this great since you have near Z simple syncs already?
Let me show you how to add the service to our modular application.
In my Goulding's module, I declare an interface was name service status log that contains only one
massive print service that is slow.
I have implementation of this interface.
Is it the school default service status slogan for the sake of our demo and just brings text to console?
Now, in my model info file, I need to declare that this module provides such service was a complete
implementation.
I use provides service directive for.
This is the next logical step would be to consumes this service in my app module.
I use uses directly for that and specify full class name of the interface.
After that, in App G+ with the help of Service Loader from January to package I can load my service.
I use load mass and quasi loss of the services that I need.
Service loader is a terrible time.
That's why it has iterate mass app that allows me to get a reference to the first service load.
The object in the sequence handles a reference to the object.
I can call print service that is massive.
Let's now use our new brush to compile new sources one more time and run our app in so you can see that
we successfully managed to invoke service message.
That's all what I wanted to share with you in this lesson.
Let's recap what we have learned in this lesson.
In the lesson we have learned what general models are all seeing.
Those are goals that we want to achieve and that that captures images.
376.
We have learned different more of those types of after that revealed details.
Different command line options to work was more use and on real example, you saw how to create, build
and run modular application.
Together, we can simulate eclipse.
The work was modular application form I.D. and the zenith of the lesson I explained to you how to provide
and consume service in different modules.
Thanks a lot for your attention.
Have a great day and see you in the next lesson.