Home » Java » Core Java » Java 9: Welcome to Module world

About Ramesh Kotha

Ramesh Kotha
Ramesh is a certified Java professional working in financial domain, He is an application developer and has hands on practice with Java and related technologies. He is passionate about Spring and other open source technologies.

Java 9: Welcome to Module world

Java 9 has been released on September 21 officially, Eclipse is supporting Java 9 from  Eclipse Oxygen.1a (4.7.1a), Lets jump into module world..!!!

Download Java 9 from here, and add it to Eclipse Installed JRE’s as below
 
 
 
 
 
 
 
 
 

That’s it, we are good to write Java 9 module programs in Eclipse.

  1. Create First java project and add module-info.java to it, right click on the project

module-info.java

module first {

}

Module should start with keyword module followed by its name. currently it doesn’t requires anything or it doesn’t export anything.

2. Let’s create Second java project,

module-info.java

module second {
exports second; --<em> second module is exporting a package "second"</em>
}

Second.java — create a simple Java class with public sayHello() method in second package

package second;

public class Second {
public void sayHello() {
System.out.println("Welcome to module world..!!");
}
}

Second java class will be available to other modules, as it is exporting second package.

3. Update first module –  module-info.java as below

module first {
requires second;
}

Now first module requires second module, but it will fail with module can not be resolved compilation error

“second cannot be resolved to a module”

we need to add second module as dependency to first module.

Check the new thing called modulepath

3. Create FirstTest.java in first package as below

package first;

import second.Second; // we are accessing Second.java from second module

public class FirstTest {

public static void main(String[] args) {
Second second = new Second();
second.sayHello();
}
}

Running the above would print “Welcome to module world..!!

Source code can be found at Github

Published on Java Code Geeks with permission by Ramesh Kotha, partner at our JCG program. See the original article here: Java9: Welcome to Module world

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of