Hi everyone.
Welcome back.
In this lecture we are going to be learning about a new concept named Unnamed Module.
So unnamed module is how pretty much most of the projects work.
Let's say if you are already building on some kind of, uh, Java application, it could be a spring
boot application or any other kind of framework.
You are executing your code in your modern Java platform, maybe a Java 11 or Java 17, whatever that
might be.
You are running your code as an unnamed module.
What that means is that let's say you are executing your code in the modern Java platform, and if you
are not providing any module descriptor file like a module-info.java file, that means your code is
being executed as a unnamed module.
So in this lecture we'll go ahead and explore that one.
For this specific lecture, we are going to be working on the module four module.
So in this case this is a very simple module.
And if you take a look at it it has a main class and the main class.
What it does is that it's going to print you the module name.
Okay.
Pretty much every module that you have worked on so far will have this main class.
There is a reason why I have that class will be leveraging that in this specific lecture.
So now what I'm going to do here is that so for this specific class we don't have any module file.
Right.
But when you execute this main class it's going to have access to this module name.
And then we are going to see what is a value.
It is going to print.
So to start with what I'm going to do I'm going to open the Gradle section over here and then go to
the module for in the module for what we will do, we'll go ahead and execute the build task.
So what this build task is going to do.
This is going to take care of building the project for you.
And then it is going to place the jar file in the libs directory.
So in this case what we will do I have the terminal open here.
So we are going to be navigating to the module four.
So CD modules module four.
There you go.
And then do a ls.
So this is where the build directory and everything is here.
So now what I'm going to do I'm going to execute the jar file.
And while I'm executing the jar file I'm going to execute this main class okay.
So let's go ahead and do the execution of this one.
In this case Java hyphen hyphen Classpaths.
So this is how we normally launch a Jar file, or execute a class that's inside a Jar file.
And then you provide a class path, and then you provide the path where the jar file is present.
In this case it's going to be build slash libs.
And then we provide the module for that jar file.
And then we provide the class name.
So in this case the class is going to be com dot module four dot main.
So when you execute this code what we are doing here is that we are going to statically print this value.
But it's going to give you a module name that gets created automatically for you.
Right.
If you press enter here you see right there is something called an unnamed module.
So this particular module, when you execute in a modern JDK platform, anything after Java nine, those
classes or applications are still run as a module, but in this case it's called an unnamed module.
This is specifically done for us, just to make sure the portability of executing the Non-modular code
in the modular Java platform.
So that's the main purpose of introducing this unnamed module concept.
In addition to the classpath, we have another concept called module path.
So how do we execute a code in a module path.
So all you got to do is we're going to use a somewhat similar command.
But in here we're going to change a few things.
So in this case the very first thing is I'm going to provide something called a module path okay.
So let's go ahead and create the module path.
And then you provide the module after this one.
So we have the module path.
And this is a directory where all the modules are present for the specific application.
And then you provide a module name by using the hyphen m flag.
And then the module name is going to be module four.
And then slash comm dot module four dot main.
So if you execute this class you're going to see a different result.
Press enter over here you see right.
So in this case it was able to automatically add a name for the specific module.
So this concept is called automatic modules.
Unnamed module and automatic module are specifically used.
If you are going to be running a non-modular code in a module path or in a modern Java platform.
So this is a concept.
Any time you are, you may be running your applications already in a JDK 17 or JDK 21, which is the
latest if you're wondering how this code is getting run in that environment.
If you don't have any module file in it, if you're just using the classpath, then those code are still
being bundled and run as an unnamed module.
When you're executing your code in an unnamed module, what happens is that it's going to have visibility
to all the platform modules.
In this case, this unnamed module will have access to all the modules that we have over here.
So whatever you see here, all these modules will be visible to that unnamed module.
I hope you all have a pretty good idea about how the unnamed module works, and how the code that we
write today without the module file gets executed in the modern Java platform.
If you are running your code without the module file, you can still continue to do it without any issues.
But this is for your understanding.
Even though we are not providing any module file, your code is still being bundled and executed as
an unnamed module.
This marks the end of this lecture.
Thank you for watching.