Simple Module application in Java 10 and Eclipse Oxygen 3a
In this post I show how in very simple way create module application in Java 10
using Eclipse IDE(Oxygen 3a)
.
Open IDE and select from main menu File->New->Java Project
.
In the New Java Project
window in Project Name
field write name of the module. Convention of naming module is the same as package: domain plus other words. This module I named with m1 at the end,
Click the Finish
button. In Package Explorer
right click name your module(project name) and choose New->Class
to create your first class in this project.
In the New Java Class
window in Package
field write name of package for this class. In the Name
field write name of the class.
Click the Finish
button. In Hello.java file write method return String. I named this method sayHello.
OK. In this module you have one class: Hello. Now we have to create module descriptor. Each module must have one module descriptor(module-info.java). Right click the module name and choose Configure->Create module-info.java
.
In New module-info.java
window in Module name
field write name of module. Default name may be correct.
Click the Finish
button.
In module body write exports with name of package with the class. This package you want to export to outside to use through other modules.
In next step create other module(project). Open IDE, select from main menu File->New->Java Project
.
In the New Java Project
window in Project Name
field write name of the module. Convention of naming module is the same as package: domain plus other words. This module I named with m2 at the end.
Click the Finish
button.
Now we have to create module descriptor. Each module must have one module descriptor(module-info.java). Right click the module name and choose Configure->Create module-info.java
.
In New module-info.java
window in Module name
field write name of module. Default name may be correct.
Click the Finish
button. In body of this module write requires
and name of the module. This module m1 need module m2 to use packages. Our m2 module use m1 module, which delivers net.doraprojects.hello package with Hello class.
Create main class in m2 module: HelloClient.
Click the Finish
button.
In main method this class create instance Hello
class from m1
module and net.doraprojects.hello
package. Call sayHello
method this object in System.out.print
method.
In third step you have to connect these modules. The m2
module need m1
module to use Hello
object. So right click the name of the m2
module and select Properties
.
In Properties
window select Java Build Path
, go to Projects
tab and click the Modulepath
.
Click the Add
button. Select name of module to add and click OK
button.
Click the Apply
button and the Apply and Close
button.
Select main class and Run it.
In Output
window you see text come from m1 module from Hello object.