Java 9's JPMS Basic Features (requires and exports) for Creating Modules

Basics of Java 9 Modularity (JPMS or Project Jigsaw) Chapter 3- Introducing Java 9's JPMS Basic Features for Modular Programming
8 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€66.45
List Price:  €94.93
You save:  €28.48
£55.32
List Price:  £79.03
You save:  £23.71
CA$98.04
List Price:  CA$140.06
You save:  CA$42.02
A$107.72
List Price:  A$153.89
You save:  A$46.17
S$94.09
List Price:  S$134.42
You save:  S$40.33
HK$544.68
List Price:  HK$778.15
You save:  HK$233.46
CHF 61.92
List Price:  CHF 88.46
You save:  CHF 26.54
NOK kr774.79
List Price:  NOK kr1,106.89
You save:  NOK kr332.10
DKK kr495.67
List Price:  DKK kr708.13
You save:  DKK kr212.46
NZ$118.82
List Price:  NZ$169.76
You save:  NZ$50.93
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,339.40
List Price:  ৳11,913.94
You save:  ৳3,574.54
₹5,914.23
List Price:  ₹8,449.26
You save:  ₹2,535.03
RM311.24
List Price:  RM444.65
You save:  RM133.41
₦118,083.62
List Price:  ₦168,698.12
You save:  ₦50,614.50
₨19,391.36
List Price:  ₨27,703.14
You save:  ₨8,311.77
฿2,410.17
List Price:  ฿3,443.25
You save:  ฿1,033.08
₺2,423.71
List Price:  ₺3,462.60
You save:  ₺1,038.88
B$418.75
List Price:  B$598.25
You save:  B$179.49
R1,273.04
List Price:  R1,818.71
You save:  R545.66
Лв129.87
List Price:  Лв185.54
You save:  Лв55.66
₩97,732.12
List Price:  ₩139,623.30
You save:  ₩41,891.18
₪255.55
List Price:  ₪365.09
You save:  ₪109.53
₱4,110.61
List Price:  ₱5,872.56
You save:  ₱1,761.94
¥10,630.43
List Price:  ¥15,186.98
You save:  ¥4,556.55
MX$1,418.07
List Price:  MX$2,025.91
You save:  MX$607.83
QR254.36
List Price:  QR363.39
You save:  QR109.02
P953.37
List Price:  P1,362.02
You save:  P408.64
KSh9,063.70
List Price:  KSh12,948.70
You save:  KSh3,885
E£3,478.89
List Price:  E£4,970.06
You save:  E£1,491.16
ብር8,820.94
List Price:  ብር12,601.88
You save:  ብር3,780.94
Kz63,759.63
List Price:  Kz91,089.09
You save:  Kz27,329.46
CLP$68,404.72
List Price:  CLP$97,725.22
You save:  CLP$29,320.50
CN¥507.53
List Price:  CN¥725.08
You save:  CN¥217.54
RD$4,206.74
List Price:  RD$6,009.89
You save:  RD$1,803.14
DA9,351.13
List Price:  DA13,359.34
You save:  DA4,008.20
FJ$158.85
List Price:  FJ$226.94
You save:  FJ$68.09
Q538.41
List Price:  Q769.20
You save:  Q230.78
GY$14,600.65
List Price:  GY$20,858.96
You save:  GY$6,258.31
ISK kr9,615.92
List Price:  ISK kr13,737.62
You save:  ISK kr4,121.70
DH699.24
List Price:  DH998.96
You save:  DH299.72
L1,281.52
List Price:  L1,830.82
You save:  L549.30
ден4,084.06
List Price:  ден5,834.63
You save:  ден1,750.56
MOP$559.24
List Price:  MOP$798.95
You save:  MOP$239.71
N$1,266.15
List Price:  N$1,808.86
You save:  N$542.71
C$2,568.06
List Price:  C$3,668.81
You save:  C$1,100.75
रु9,427.87
List Price:  रु13,468.97
You save:  रु4,041.09
S/262.66
List Price:  S/375.25
You save:  S/112.58
K281.39
List Price:  K402.01
You save:  K120.61
SAR262.91
List Price:  SAR375.60
You save:  SAR112.69
ZK1,903.42
List Price:  ZK2,719.29
You save:  ZK815.86
L330.70
List Price:  L472.45
You save:  L141.75
Kč1,679.39
List Price:  Kč2,399.23
You save:  Kč719.84
Ft27,552.80
List Price:  Ft39,362.83
You save:  Ft11,810.03
SEK kr765.72
List Price:  SEK kr1,093.94
You save:  SEK kr328.21
ARS$70,549.59
List Price:  ARS$100,789.45
You save:  ARS$30,239.85
Bs482.26
List Price:  Bs688.98
You save:  Bs206.71
COP$307,907.55
List Price:  COP$439,886.78
You save:  COP$131,979.23
₡35,643.95
List Price:  ₡50,922.11
You save:  ₡15,278.16
L1,765.02
List Price:  L2,521.57
You save:  L756.54
₲545,488.80
List Price:  ₲779,303.12
You save:  ₲233,814.31
$U2,997.83
List Price:  $U4,282.79
You save:  $U1,284.96
zł286.31
List Price:  zł409.03
You save:  zł122.72
Already have an account? Log In

Transcript

In this video, I'm going to show you how you could create a modular project using Java nine pliers and then use JP ms features in order to help us solve the problems we had in the previous videos. So as usual inside Eclipse IDE, you can go to the File menu, and then click New Java project. And then in here, choose your project name for me, let's just keep it simple. I'm going to be selecting module one as the name and what you need to be careful of is that these JRE version should be anything after Java nine for me, it's going to be Java 11, which is anyway nine plus and it could be used if I want to have a modular project which uses the features of jpm s. I'm going to accept all the defaults and click Next. The first thing you might find new is this module dash in for the Java file is going to be created for us you could check off these want to be created by Eclipse ID or you could do it on your own later on but I prefer to check off this one and these fine as the name implies is like a class but it's not actually a Class is going to be a container for information related to our module and we are going to be talking about that one later.

The other things you find new is that under the Projects tab, and the libraries tab, as you see in here, we have these module path added to what we previously had, we already had class path. But starting Java nine, we have this module pattern. As you see in here, there's something nice, the runtime library has been added on there, the module path, and again, later on, we're going to provide you with more details. So let's accept our chain and click Finish. And then it asks you for a module name. And as you see in here, it says module names usually start with a lowercase letter.

So for the sake of simplicity, let's again, keep it simple this time, and my name would be module one. And as you see in here, this container is going to be created for me. And I'm going to put the information related to my module on inside these curly braces. For now let's just keep it empty. And now let's create a new package. As you remember, we said more Or something higher than the packages.

And what we desire in modular programming in Java is that we could have some kind of hierarchy. So that lets us choose what packages should be visible to the outside world. So inside my modular project, I'm going to be creating a new package. And let's say because this package is going to be used as a library, which is needed by the end user, I name it comm that is to catch that library, that user then I click finish, and my package is built for me. Now inside the package again, right click and make a new class this time and as this package contains the classes needed by the user, let's just assume we have a class which has name needed by user. Then I click finish, and we have these classes for now.

We don't need to put anything inside this class. We just like because the future is there. jpm s gives us and now let's assume we need another package in this module, which is going to be just used by the admins of the easy To catch websites there's going to be something internally used in this package doesn't need to be shown or used by the end user. So I name it comm that is it to catch that library dot internal this time, and I just click Finish. And inside this package, I create another class, which is again, as mentioned is going to be used just internally. So I name this one internally needed.

Just as simple as I want. It doesn't need to have anything for now just click Finish. Now as you see, we have two packages in our modular project. And in each package, we have one class by the way, as mentioned before, we have named this package module one, but since we have these two packages, probably as it's the best practice in the industry, it's best to name these one calm that easy to catch track library, like the common prefix that both these packages share. But for now, again, let's just keep it simple as it is. Let's just name it just module one.

I'm going to continue by creating a new module. So this time again, a new project. I name it module two Again, make sure Java nine plus is selected, I just click finish and then the name of the module again, as simple as possible just module two, I create the module. And as you see a new module has been created for me. Now, let's say this is the module used by the end user. As usual, I create a new package and I name this one calm that easy to catch dot user.

And then inside the package, I create a new class, and I named that one, just user click Finish. And as you see in here, I have a new class in my new package in my new module so far, you know the rules. If I am inside this class user, and I need to access any of those two files in those two packages in the other module, what do we need to do is to import those packages. So what comes to mind is that we should have these kind of line that I want to import these needed by user class from that package, but as you see in here, we have this red line it says this package cannot resolve. The problem is we want to access the package from Another module in here is the point that modules are going to be really helpful. The first thing I need to do is that I should go inside the module into the Java from the module.

And inside the curly braces, I should explicitly say that I require the other module. So let's say as an example, I should say, I as the module two require to use module one. So as you see, we have a new keyboard requires which is going to be used for the modules which module requires the other module. But even if you save as we have done now, then you see another problem arises, and this time it says Module One can't resolve to a module. So what happens in here is there module two requires module one, but at the same time, Module One is the module which should give the permission to Module Two to be used by that. So here we're going to make you familiar with the other keyword we used inside the modules which is going to be exports.

So in here, we're going to be going back to Module dash into the Java file. For the first module in here we should explicit We say which packages are going to be exported to the outer world. Basically, we give permissions to which packages inside this module to be visible and use from the outer world outside this module. So let's say in this case, I want to say I want to have the option to export these column that is the cache that library do the user justice package, not the other package. So now after you give the permission to export, and then you save everything, the last step you need to do is basically go to the module two, and then right click on that one, go to the build path, and configure build path. And on here, under the project's tab, select the module path and then add module one as the module which is going to be required by this module two, so I'm going to be clicking OK and then apply and as you see, the problem is going to be solved now here we have explicitly said this module two requires module one and at the same time in the same file for module one, we have said the Outer modules would have permission to access just this package inside this module one, in this case, we have the module two dependent on the module one, and we have the module one, which gives access to this package and just this package to the other modules.

So based on what we have talked so far, you could go to the user class. So you could say I want to create an object of type needed by user. And then you could create your object, let's say the name is needed. And I'm going to be just calling the constructor. And that's it. As you see it's very effective, although inside module to use that I require module one.

But inside module one, the module info says, I'm just exporting this package, it means that you don't have any option to be inside this user class into module two, and then create an object of type internally needed class. If you want to do that one is going to be like that. So I'm going to say internally needed and it's not recognized. Even if Come here and have these kind of imports to mention I need the internally needed class from the other package as comm that easy to catch that library that internal. As you see that gives you an error if you hover the mouse on here it says this type internal invalid class is not accessible. And it's a nice feature as you see in here we could have modules requiring other modules and the required module is going to be giving the permission to the other modules which packages could be used by the requesting module and that could be done by the use of these exports keyword.

So nice job it seems that we have used the access modifiers at the level of packages. In the next video, we're going to talk about how we could use these features in the examples that we had in the previous video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.