Sunday, 16 June 2019

Java9: Working with two or more modules


In my previous post, I explained how to create run ‘Hello World’ application using java9 modularity. In this post, I am going to explain how can you work with two or more modules together.

As you see above image, there are two modules.
a. main
b. math.arithmetic

'main.arithmetic' module contains all the packages and classes that are used to perform arithmetic operations. 'main' module depends on 'main.arithmetic' module to compute arithmetic operations.

Step 1: Create main.arithmetic module.
Create ArithmeticUtil.java and module-info.java files by following below directory structure.

$tree schoolmath/
schoolmath/
└── src
    └── math.arithmetic
        ├── com
        │   └── sample
        │       └── util
        │           └── ArithmeticUtil.java
        └── module-info.java


ArithmeticUtil.java
package com.sample.util;

public class ArithmeticUtil {

    public static int add(int a, int b) {
        return a + b;
    }

    public static int sub(int a, int b) {
        return a - b;
    }

    public static int mul(int a, int b) {
        return a * b;
    }

    public static int div(int a, int b) {
        return a / b;
    }

}


module-info.java
module math.arithmetic{
   exports com.sample.util;
}

Above statement tells 'com.sample.util' package can be exposed outside of the module.


Step 2: Define 'main' module by following below directory structure.
$tree schoolmath/
schoolmath/
└── src
    ├── main
    │   ├── com
    │   │   └── sample
    │   │       └── app
    │   │           └── App.java
    │   └── module-info.java
    └── math.arithmetic
        ├── com
        │   └── sample
        │       └── util
        │           └── ArithmeticUtil.java
        └── module-info.java

9 directories, 4 files


App.java
package com.sample.app;

import com.sample.util.ArithmeticUtil;

public class App {

    public static void main(String args[]) {

        System.out.println("Sum of 10 and 20 is : " + ArithmeticUtil.add(10, 20));
        System.out.println("Subtraction of 10 and 20 is : " + ArithmeticUtil.sub(10, 20));
        System.out.println("Multiplication of 10 and 20 is : " + ArithmeticUtil.mul(10, 20));
        System.out.println("Division of 10 and 20 is : " + ArithmeticUtil.div(10, 20));

    }
}


module-info.java
module main{
    requires math.arithmetic;
}

Above snippet tells that, main module depends on 'math.arithmetic' module.

Step 3: Compile and run App.java.
go inside schoolmath directory and execute below command to compile the classes.

javac --module-source-path src -d out src/main/com/sample/app/App.java
$javac --module-source-path src -d out src/main/com/sample/app/App.java
$
$tree
.
├── out
│   ├── main
│   │   ├── com
│   │   │   └── sample
│   │   │       └── app
│   │   │           └── App.class
│   │   └── module-info.class
│   └── math.arithmetic
│       ├── com
│       │   └── sample
│       │       └── util
│       │           └── ArithmeticUtil.class
│       └── module-info.class
└── src
    ├── main
    │   ├── com
    │   │   └── sample
    │   │       └── app
    │   │           └── App.java
    │   └── module-info.java
    └── math.arithmetic
        ├── com
        │   └── sample
        │       └── util
        │           └── ArithmeticUtil.java
        └── module-info.java

18 directories, 8 files

Execute below command to run the application.
java --module-path out -m main/com.sample.app.App

$java --module-path out -m main/com.sample.app.App
Sum of 10 and 20 is : 30
Subtraction of 10 and 20 is : -10
Multiplication of 10 and 20 is : 200
Division of 10 and 20 is : 0

You can get complete working application here.




Previous                                                 Next                                                 Home

No comments:

Post a Comment