WEBVTT

00:00.080 --> 00:00.650
Hi everyone.

00:00.650 --> 00:01.130
Welcome back.

00:01.130 --> 00:05.240
In this lecture we are going to be learning about a new concept named Unnamed Module.

00:05.240 --> 00:08.810
So unnamed module is how pretty much most of the projects work.

00:08.810 --> 00:13.190
Let's say if you are already building on some kind of, uh, Java application, it could be a spring

00:13.190 --> 00:15.650
boot application or any other kind of framework.

00:15.680 --> 00:23.420
You are executing your code in your modern Java platform, maybe a Java 11 or Java 17, whatever that

00:23.420 --> 00:24.050
might be.

00:24.080 --> 00:27.260
You are running your code as an unnamed module.

00:27.380 --> 00:32.660
What that means is that let's say you are executing your code in the modern Java platform, and if you

00:32.690 --> 00:38.360
are not providing any module descriptor file like a module-info.java file, that means your code is

00:38.390 --> 00:41.120
being executed as a unnamed module.

00:41.120 --> 00:43.760
So in this lecture we'll go ahead and explore that one.

00:43.760 --> 00:48.380
For this specific lecture, we are going to be working on the module four module.

00:48.380 --> 00:51.140
So in this case this is a very simple module.

00:51.140 --> 00:53.960
And if you take a look at it it has a main class and the main class.

00:53.960 --> 00:57.350
What it does is that it's going to print you the module name.

00:57.380 --> 00:57.800
Okay.

00:57.830 --> 01:02.270
Pretty much every module that you have worked on so far will have this main class.

01:02.270 --> 01:06.930
There is a reason why I have that class will be leveraging that in this specific lecture.

01:06.930 --> 01:12.420
So now what I'm going to do here is that so for this specific class we don't have any module file.

01:12.450 --> 01:12.960
Right.

01:12.960 --> 01:18.360
But when you execute this main class it's going to have access to this module name.

01:18.360 --> 01:20.550
And then we are going to see what is a value.

01:20.550 --> 01:21.750
It is going to print.

01:22.080 --> 01:27.330
So to start with what I'm going to do I'm going to open the Gradle section over here and then go to

01:27.360 --> 01:33.600
the module for in the module for what we will do, we'll go ahead and execute the build task.

01:33.600 --> 01:35.580
So what this build task is going to do.

01:35.610 --> 01:38.370
This is going to take care of building the project for you.

01:38.370 --> 01:41.790
And then it is going to place the jar file in the libs directory.

01:41.790 --> 01:44.700
So in this case what we will do I have the terminal open here.

01:44.700 --> 01:47.190
So we are going to be navigating to the module four.

01:47.220 --> 01:50.940
So CD modules module four.

01:50.970 --> 01:51.690
There you go.

01:51.690 --> 01:52.560
And then do a ls.

01:52.560 --> 01:55.350
So this is where the build directory and everything is here.

01:55.350 --> 01:58.590
So now what I'm going to do I'm going to execute the jar file.

01:58.590 --> 02:03.240
And while I'm executing the jar file I'm going to execute this main class okay.

02:03.270 --> 02:06.210
So let's go ahead and do the execution of this one.

02:06.240 --> 02:09.130
In this case Java hyphen hyphen Classpaths.

02:09.130 --> 02:14.440
So this is how we normally launch a Jar file, or execute a class that's inside a Jar file.

02:14.440 --> 02:18.730
And then you provide a class path, and then you provide the path where the jar file is present.

02:18.730 --> 02:22.210
In this case it's going to be build slash libs.

02:22.210 --> 02:25.150
And then we provide the module for that jar file.

02:25.150 --> 02:27.370
And then we provide the class name.

02:27.370 --> 02:33.580
So in this case the class is going to be com dot module four dot main.

02:33.580 --> 02:38.530
So when you execute this code what we are doing here is that we are going to statically print this value.

02:38.530 --> 02:43.300
But it's going to give you a module name that gets created automatically for you.

02:43.330 --> 02:43.840
Right.

02:43.870 --> 02:49.750
If you press enter here you see right there is something called an unnamed module.

02:49.750 --> 02:57.130
So this particular module, when you execute in a modern JDK platform, anything after Java nine, those

02:57.130 --> 03:03.430
classes or applications are still run as a module, but in this case it's called an unnamed module.

03:03.430 --> 03:10.510
This is specifically done for us, just to make sure the portability of executing the Non-modular code

03:10.510 --> 03:13.080
in the modular Java platform.

03:13.080 --> 03:17.040
So that's the main purpose of introducing this unnamed module concept.

03:17.280 --> 03:20.460
In addition to the classpath, we have another concept called module path.

03:20.460 --> 03:22.800
So how do we execute a code in a module path.

03:22.800 --> 03:26.430
So all you got to do is we're going to use a somewhat similar command.

03:26.430 --> 03:29.940
But in here we're going to change a few things.

03:29.940 --> 03:35.700
So in this case the very first thing is I'm going to provide something called a module path okay.

03:35.730 --> 03:37.200
So let's go ahead and create the module path.

03:37.200 --> 03:39.930
And then you provide the module after this one.

03:39.930 --> 03:41.250
So we have the module path.

03:41.250 --> 03:45.660
And this is a directory where all the modules are present for the specific application.

03:45.660 --> 03:49.290
And then you provide a module name by using the hyphen m flag.

03:49.290 --> 03:52.170
And then the module name is going to be module four.

03:52.170 --> 03:55.560
And then slash comm dot module four dot main.

03:55.560 --> 03:58.200
So if you execute this class you're going to see a different result.

03:58.200 --> 04:00.000
Press enter over here you see right.

04:00.030 --> 04:04.860
So in this case it was able to automatically add a name for the specific module.

04:04.860 --> 04:07.860
So this concept is called automatic modules.

04:07.890 --> 04:10.830
Unnamed module and automatic module are specifically used.

04:10.830 --> 04:17.560
If you are going to be running a non-modular code in a module path or in a modern Java platform.

04:17.560 --> 04:18.640
So this is a concept.

04:18.640 --> 04:24.370
Any time you are, you may be running your applications already in a JDK 17 or JDK 21, which is the

04:24.370 --> 04:28.750
latest if you're wondering how this code is getting run in that environment.

04:28.750 --> 04:34.270
If you don't have any module file in it, if you're just using the classpath, then those code are still

04:34.300 --> 04:37.210
being bundled and run as an unnamed module.

04:37.210 --> 04:42.280
When you're executing your code in an unnamed module, what happens is that it's going to have visibility

04:42.280 --> 04:44.350
to all the platform modules.

04:44.350 --> 04:49.450
In this case, this unnamed module will have access to all the modules that we have over here.

04:49.450 --> 04:54.430
So whatever you see here, all these modules will be visible to that unnamed module.

04:54.460 --> 05:00.070
I hope you all have a pretty good idea about how the unnamed module works, and how the code that we

05:00.070 --> 05:04.360
write today without the module file gets executed in the modern Java platform.

05:04.360 --> 05:09.700
If you are running your code without the module file, you can still continue to do it without any issues.

05:09.700 --> 05:11.350
But this is for your understanding.

05:11.350 --> 05:16.900
Even though we are not providing any module file, your code is still being bundled and executed as

05:16.900 --> 05:18.550
an unnamed module.

05:18.580 --> 05:19.930
This marks the end of this lecture.

05:19.930 --> 05:21.070
Thank you for watching.
