Getting Started with Maven | Creating a Maven Project | Running the Program

After running package, open the project folder. A new folder named target should have been created. Inside it, there is a file named SampleMavenApp-1.0-SNAPSHOT.jar. This is the Jar file generated by the build.

Now run this file. In a command prompt or terminal, run cd target to move into the target directory. Then execute the following command.

$ java -classpath SampleMavenApp-1.0-SNAPSHOT.jar com.devkuma.App

This runs the com.devkuma.App class from SampleMavenApp-1.0-SNAPSHOT.jar. The following text is printed.

Hello World!

This is the execution result of the com.devkuma.App class. The App.java source code is written as follows.

package com.devkuma;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

The Jar Cannot Be Run by Itself

Specifying the Jar file with -classpath and specifying the main class is an old-style way to run a generated Jar file.

In fact, the Jar file created by mvn package is not an executable Jar. That is why this extra command form is required.

There is also a more understandable way to build and run a project. However, it requires a dedicated plugin, which will be explained later. Until then, check the program by creating a package with mvn package and running it with java -classpath.