Create a Java project with Maven

Prerequisites

Java and Maven -> See How to install Java and Maven on MacOS, Linux and Windows

Create a new Java project with Maven

Open a terminal and run:

mvn archetype:generate \
    -DgroupId=com.mycompany.app \
    -DartifactId=my-app \
    -DarchetypeArtifactId=maven-archetype-quickstart \
    -DarchetypeVersion=1.4 \
    -DinteractiveMode=false

This command will create a folder with the same name as the artifactId and the following structure:

my-app
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java

You can change the target & source compiler versions:

 <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>17</source>
          <target>17</target>
        </configuration>
      </plugin>
</plugins>

source: https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

Build the project

Run:

mvn package

Run the built app:

java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App