Sunday 16 June 2019

Java9: modularity: Hello World Application


In this post, I am going to build helloworld application, it has a module named ‘printdata’. ‘printdata’ module has a package ‘com.sample.app’, which has Main class that prints ‘Hello World’ message to console.

Step 1: Create Main.java file by following below directory structure.
$tree
.
└── helloworld
    └── src
        └── printdata
            └── com
                └── sample
                    └── app
                        └── Main.java

6 directories, 1 file

‘helloworld’ : Application name.
‘src’ All the modules will go here.
‘printdata’ : Module name.
com.sample.app : Package name

Main.java
package com.sample.app;

public class Main{
   public static void main(String args[]){
      System.out.println("Hello World");
   }
}

Step 2: Create module-info.java file under the folder printdata.


module-info.java
module printdata{
   requires java.base;
}

$tree
.
└── helloworld
    └── src
        └── printdata
            ├── com
            │   └── sample
            │       └── app
            │           └── Main.java
            └── module-info.java

6 directories, 2 files


Step 3: Compile Main.java
Execute below command from ‘helloworld’ folder.
javac --module-source-path src -d out src/printdata/com/sample/app/Main.java
$javac --module-source-path src -d out src/printdata/com/sample/app/Main.java
$
$tree
.
├── out
│   └── printdata
│       ├── com
│       │   └── sample
│       │       └── app
│       │           └── Main.class
│       └── module-info.class
└── src
    └── printdata
        ├── com
        │   └── sample
        │       └── app
        │           └── Main.java
        └── module-info.java

10 directories, 4 files

Step 4: Run Main.java application.
Execute below command from ‘helloworld’ folder.

java --module-path out -m printdata/com.sample.app.Main
$java --module-path out -m printdata/com.sample.app.Main
Hello World

You can download complete working application from below location.





Previous                                                 Next                                                 Home

No comments:

Post a Comment