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
€64.99
List Price:  €92.85
You save:  €27.85
£55.77
List Price:  £79.68
You save:  £23.90
CA$95.68
List Price:  CA$136.70
You save:  CA$41.01
A$105.96
List Price:  A$151.38
You save:  A$45.42
S$94.48
List Price:  S$134.98
You save:  S$40.50
HK$546.78
List Price:  HK$781.15
You save:  HK$234.36
CHF 63.33
List Price:  CHF 90.48
You save:  CHF 27.14
NOK kr761.11
List Price:  NOK kr1,087.35
You save:  NOK kr326.23
DKK kr485.02
List Price:  DKK kr692.92
You save:  DKK kr207.89
NZ$116.44
List Price:  NZ$166.35
You save:  NZ$49.91
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,660.01
List Price:  ৳10,943.35
You save:  ৳3,283.33
₹5,835.78
List Price:  ₹8,337.18
You save:  ₹2,501.40
RM331.75
List Price:  RM473.95
You save:  RM142.20
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,492.21
List Price:  ₨27,847.21
You save:  ₨8,355
฿2,572.74
List Price:  ฿3,675.50
You save:  ฿1,102.76
₺2,264.43
List Price:  ₺3,235.04
You save:  ₺970.61
B$355.02
List Price:  B$507.19
You save:  B$152.17
R1,295.44
List Price:  R1,850.71
You save:  R555.27
Лв127.05
List Price:  Лв181.51
You save:  Лв54.46
₩94,909.58
List Price:  ₩135,590.93
You save:  ₩40,681.35
₪260.15
List Price:  ₪371.66
You save:  ₪111.51
₱3,993.87
List Price:  ₱5,705.78
You save:  ₱1,711.90
¥10,713.01
List Price:  ¥15,304.96
You save:  ¥4,591.95
MX$1,187.73
List Price:  MX$1,696.83
You save:  MX$509.10
QR254.83
List Price:  QR364.06
You save:  QR109.23
P950.82
List Price:  P1,358.38
You save:  P407.55
KSh9,247.76
List Price:  KSh13,211.65
You save:  KSh3,963.89
E£3,352.12
List Price:  E£4,788.95
You save:  E£1,436.83
ብር4,006.43
List Price:  ብር5,723.72
You save:  ብር1,717.28
Kz58,511.64
List Price:  Kz83,591.64
You save:  Kz25,080
CLP$65,950.47
List Price:  CLP$94,219
You save:  CLP$28,268.52
CN¥506.53
List Price:  CN¥723.65
You save:  CN¥217.11
RD$4,055.76
List Price:  RD$5,794.19
You save:  RD$1,738.43
DA9,420.16
List Price:  DA13,457.95
You save:  DA4,037.79
FJ$157.70
List Price:  FJ$225.30
You save:  FJ$67.59
Q542.52
List Price:  Q775.06
You save:  Q232.54
GY$14,601.52
List Price:  GY$20,860.22
You save:  GY$6,258.69
ISK kr9,773.40
List Price:  ISK kr13,962.60
You save:  ISK kr4,189.20
DH703.98
List Price:  DH1,005.73
You save:  DH301.75
L1,236.34
List Price:  L1,766.28
You save:  L529.93
ден3,998.26
List Price:  ден5,712.05
You save:  ден1,713.78
MOP$561.77
List Price:  MOP$802.57
You save:  MOP$240.79
N$1,291.99
List Price:  N$1,845.78
You save:  N$553.78
C$2,569.36
List Price:  C$3,670.67
You save:  C$1,101.31
रु9,319.09
List Price:  रु13,313.56
You save:  रु3,994.46
S/260.54
List Price:  S/372.22
You save:  S/111.67
K269.79
List Price:  K385.44
You save:  K115.64
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,882.68
List Price:  ZK2,689.66
You save:  ZK806.98
L323.40
List Price:  L462.03
You save:  L138.62
Kč1,628.77
List Price:  Kč2,326.92
You save:  Kč698.14
Ft25,334.28
List Price:  Ft36,193.38
You save:  Ft10,859.10
SEK kr755.02
List Price:  SEK kr1,078.64
You save:  SEK kr323.62
ARS$61,468.17
List Price:  ARS$87,815.44
You save:  ARS$26,347.26
Bs483.33
List Price:  Bs690.51
You save:  Bs207.17
COP$271,845.87
List Price:  COP$388,367.89
You save:  COP$116,522.02
₡35,672.25
List Price:  ₡50,962.55
You save:  ₡15,290.29
L1,724.16
List Price:  L2,463.20
You save:  L739.03
₲522,510.75
List Price:  ₲746,475.93
You save:  ₲223,965.17
$U2,674.97
List Price:  $U3,821.56
You save:  $U1,146.58
zł281.37
List Price:  zł401.98
You save:  zł120.60
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.