dcsimg
November 15, 2018
Hot Topics:

Using a Java 9 Module as a JAR File

  • September 14, 2018
  • By Deepak Vohra
  • Send Email »
  • More Articles »

The Java Platform Module System (JSR 376) support is available in Java 9. In two earlier tutorials, we introduced using Java modules in "Developing a Module with Java 9 in Eclipse IDE, Part 1" and "Developing a Module with Java 9 in Eclipse IDE, Part 2." In the 2nd article, we created and used a module as a directory structure. For portability, a module may be exported to a JAR file and used as a JAR. In this tutorial, we shall create a module in a Java project in Eclipse IDE and export the Java project to a JAR file. Subsequently, we shall run the module application. This tutorial has the following sections:

Setting the Environment

Download and install an Eclipse IDE edition that supports Java 9. Eclipse IDE for Java EE Developers Version: Photon Release (4.8.0) is used in this tutorial.

Creating a Java Project

To create a Java project, select File>New>Other in Eclipse IDE. The New wizard gets started; in it, select Java>Java Project and click Next, as shown in Figure 1.

New>Java>Java Project
Figure 1: New>Java>Java Project

In the New Java Project wizard, specify a Project name (hellojigsaw) and select the option Use default location. In JRE, select Use an execution environment JRE and select JavaSE-9, as shown in Figure 2.

Selecting JRE as JavaSE-9
Figure 2: Selecting JRE as JavaSE-9

To configure the project layout, select the Create separate folders for sources and class files option and click Configure default…, as shown in Figure 3.

Project layout> Create separate folders for sources and class files>Configure default
Figure 3: Project layout> Create separate folders for sources and class files>Configure default

In Preferences for Java>Build Path, select Folders and specify Source folder name as src and Output folder name as modules/hellojigsaw, as shown in Figure 4. Click Apply and Close.

Configuring Build Path
Figure 4: Configuring Build Path

In New Java Project, click Next, as shown in Figure 5.

New Java Project>Next
Figure 5: New Java Project>Next

In Java Settings, the Source code directory structure is displayed (see Figure 6). Select the Create module-info.java file option and select Default the hellojigsaw/modules/hellojigsaw output folder, as as shown in Figure 6. Click Finish.

Java Settings
Figure 6: Java Settings

The New module-info.java window gets displayed with the Module name field, as shown in Figure 7. By default, the module name (hellojigsaw) is the same as the project name, but it could be made different. Although not a requirement, a module name should start with a lower case letter. Click Create.

New module-info.java
Figure 7: New module-info.java

A new project, hellojigsaw, gets created, as shown in Figure 8. The module-info.java source code file specifies a module declaration for the hellojigsaw module.

New Java Project hellojigsaw
Figure 8: New Java Project hellojigsaw

Creating a Module

A module consists of a module declaration file, module-info.java, and a main class that is run by using the module. The module-info.java class declares a module called hellojigsaw and is listed next:

module hellojigsaw {
}

The main class includes a main method and outputs a Hello Jigsaw message. The main class HelloJigsaw is listed.

package hellojigsaw;
public class HelloJigsaw {
   public static void main(String[] args) {
      System.out.println("Hello Jigsaw!");
   }
}

The module declaration file, module-info.java, was already created by default. Next, we shall create the main class. Select File>New>Class, as shown in Figure 9.

File>New>Class
Figure 9: File>New>Class

In the New Java Class window, the Source folder for the class is pre-specified as hellojigsaw/src, as shown in Figure 10. Specify the Package as hellojigsaw and the class name as HelloJigsaw. Select the option to create a method stub for the main method. Click Finish.

New Java Class>Finish
Figure 10: New Java Class>Finish

A new Java class, hellojigsaw.HelloJigsaw, gets created (see Figure 11). Copy the class listing to the Eclipse IDE.

Java Class Added
Figure 11: Java Class Added

Select Project>Build Automatically to build the project automatically, as shown in Figure 12.

Project>Build Automatically
Figure 12: Project>Build Automatically

Exporting the Java Project to a JAR File

In this section, we shall export the Java project to a JAR file. Right-click the hellojigsaw project in Package Explorer and select Export, as shown in Figure 13.

Package Explorer>hellojigsaw>Export
Figure 13: Package Explorer>hellojigsaw>Export

In the Export window, select Java, as shown in Figure 14.

Export>Java
Figure 14: Export>Java

Select Java>JAR file, as shown in Figure 15, and click Next.

Export>Java>JAR File
Figure 15: Export>Java>JAR File

In the JAR Export window, JAR File Specification, select the resources to export as hellojigsaw (see Figure 16). The .classpath and .project resources are selected by default. Click Browse for the JAR file to select the JAR file export destination or directory.

JAR Export
Figure 16: JAR Export

In Save As, select destination of export as hellojigsaw.jar in the hellojigsaw/modules folder, as shown in Figure 17. Click Save.

Selecting Export Folder and JAR file
Figure 17: Selecting Export Folder and JAR file

With the export JAR selected, click Finish, as shown in Figure 18.

JAR Export>Next
Figure 18: JAR Export>Next

Select the default JAR Packaging Options and click Next, as shown in Figure 19.

JAR Packaging Options
Figure 19: JAR Packaging Options

Next, select the Main class of the application entry point; for this, click the Browse in the JAR Manifest specification window, as shown in Figure 20. Keep other settings as the default.

Main Class>Browse
Figure 20: Main Class>Browse

In Select Main Class, select the HelloJigsaw class and click OK (see Figure 21).

Selecting Main Class
Figure 21: Selecting Main Class

With the main class selected, click Finish, as shown in Figure 22.

JAR Export>Finish
Figure 22: JAR Export>Finish

The Java project gets exported as hellojigsaw.jar in the modules directory, as shown in Figure 23.

Exported hellojigsaw.jar
Figure 23: Exported hellojigsaw.jar

Configuring VM Args for the Module Path

In this section, we shall configure the VM args for the module path and which module to use. Right-click the hellojigsaw project in Package Explorer and select Properties, as shown in Figure 24.

Package Explorer>hellojigsaw>Properties
Figure 24: Package Explorer>hellojigsaw>Properties

In the Properties for hellojigsaw window, select Run/Debug Settings and click New…, as shown in Figure 25, to configure a new Run configuration.

Properties>Run/Debug Settings>New…
Figure 25: Properties>Run/Debug Settings>New…

In Select Configuration Type, select Java Application, and click OK (see Figure 26).

Select Configuration Type>Java Application>OK
Figure 26: Select Configuration Type>Java Application>OK

In Edit Configuration, the Project is pre-selected as hellojigsaw. Click Search for Main class, as shown in Figure 27, to select the main class for the configuration. The main class must be the same as the main class or the entry point class of the JAR File exported.

Main class>Search
Figure 27: Main class>Search

In Select Main Type, select the HelloJigsaw class in the hellojigsaw package, as shown in Figure 28.

Selecting Main Type
Figure 28: Selecting Main Type

The main class gets selected, as shown in Figure 29.

Main Class
Figure 29: Main Class

Specify or modify the launch configuration name to Main, as shown in Figure 30. Select the Arguments tab and specify the VM arguments as follows:

--module-path modules/hellojigsaw.jar
   -m hellojigsaw/hellojigsaw.HelloJigsaw

The --module-path arg is set to the exported hellojigsaw.jar file and the <tt–m arg is set to the module main class. Click Apply to apply the launch configuration settings. Subsequently, click OK.

Edit Configuration>Apply
Figure 30: Edit Configuration>Apply

In Properties, a new Run/Debug configuration Main gets added (see Figure 31). Click Apply and Close.

New Run/Debug Configuration
Figure 31: New Run/Debug Configuration

The hellojigsaw project with exported JAR generated and run configuration created is shown in Figure 32.

Java Project hellojigsaw
Figure 32: Java Project hellojigsaw

Running the Java Module Application

To run the Java application, right-click the main class hellojigsaw.HelloJigsaw and select Run As>Java Application, as shown in Figure 33.

Package Explorer>HelloJigsaw>Run As>Java Application
Figure 33: Package Explorer>HelloJigsaw>Run As>Java Application

The module application runs and generates the output Hello Jigsaw!, as shown in Figure 34.

Module Application Output
Figure 34: Module Application Output

Conclusion

In this article, we discussed exporting a Java module project to a JAR file and using the JAR file to run a Java application.






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap

Thanks for your registration, follow us on our social networks to keep up-to-date