Java select main class

Using a Java 9 Module as a JAR File

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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 2 nd 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 data-lazy-src=

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 data-lazy-src=

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:

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 data-lazy-src=

Figure 11: Java Class Added

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

Project data-lazy-src=

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

Читайте также:  Php получить значения url

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

JAR Export data-lazy-src=

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 data-lazy-src=

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

Edit Configuration data-lazy-src=

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.

Читайте также:  Python обратиться к файлу

Package Explorer data-lazy-src=

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

Источник

Setting an Application’s Entry Point

If you have an application bundled in a JAR file, you need some way to indicate which class within the JAR file is your application’s entry point. You provide this information with the Main-Class header in the manifest, which has the general form:

The value classname is the name of the class that is your application’s entry point.

Recall that the entry point is a class having a method with signature public static void main(String[] args) .

After you have set the Main-Class header in the manifest, you then run the JAR file using the following form of the java command:

The main method of the class specified in the Main-Class header is executed.

An Example

We want to execute the main method in the class MyClass in the package MyPackage when we run the JAR file.

We first create a text file named Manifest.txt with the following contents:

Main-Class: MyPackage.MyClass

Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.

We then create a JAR file named MyJar.jar by entering the following command:

jar cfm MyJar.jar Manifest.txt MyPackage/*.class

This creates the JAR file with a manifest with the following contents:

Manifest-Version: 1.0 Created-By: 1.7.0_06 (Oracle Corporation) Main-Class: MyPackage.MyClass

When you run the JAR file with the following command, the main method of MyClass executes:

Setting an Entry Point with the JAR Tool

The ‘e’ flag (for ‘entrypoint’) creates or overrides the manifest’s Main-Class attribute. It can be used while creating or updating a JAR file. Use it to specify the application entry point without editing or creating the manifest file.
For example, this command creates app.jar where the Main-Class attribute value in the manifest is set to MyApp :

jar cfe app.jar MyApp MyApp.class

You can directly invoke this application by running the following command:

If the entrypoint class name is in a package it may use a ‘.’ (dot) character as the delimiter. For example, if Main.class is in a package called foo the entry point can be specified in the following ways:

jar cfe Main.jar foo.Main foo/Main.class

Источник

Оцените статью