Writing modular code using a command-line interface
As a tradition, let's create a simple module called helloApp, which will have a simple message and will be required by another module called helloClient. Here, we will use a command line interface to create and run the module.
Create a helloApp module folder named com.packt.helloapp and a package folder named com\packt\helloapp:
mkdir com.packt.helloapp
mkdir com.packt.helloapp\com\packt\helloapp
Now, create a HelloApp.java component class under the package name com.packt.helloapp\com\packt\helloapp and a modue-info.java file at root folder com.packt.helloapp:
HelloApp.java
package com.packt.helloapp;
public class HelloApp {
public String sayHelloJava() {
return "Hello Java 9 Module System";
}
}
module-info.java
module com.packt.helloapp {
// define exports or requires.
}
Now, we will create another module called helloClient. Create a helloClient module with the folder name com.packt.hello.client and a package with the folder name com\packt\hello\client:
mkdir com.packt.hello.client
mkdir com.packt.hello.client\com\packt\hello\client
Let's create another component class called HelloClient.java under the com.packt.hello.client\com\packt\hello\client package and create a module-info.java file at root folder com.packt.hello.client:
helloClient.java
package com.packt.hello.client;
public class HelloClient {
public static void main (String arg[]) {
//code
}
}
module-info.java
module com.packt.hello.client {
//define exports or requires
}
Both modules are independent modules so are not dependent on each other. But if we want to use a method called sayHelloJava() in the HelloClient class, then we have to import the module, otherwise it will give a compile time error package com.packt.helloapp is not visible.