- create the directory structure
mkdir -p tutorial/src/main/java/hello
then
touch tutorial/src/main/java/hello/Main.java
- put in the java file
tutorial/src/main/java/hello/Main.java
package hello;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
- create
pom.xml
at the project root (under thetutorial
directory)
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>hello</groupId>
<artifactId>java-archive</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>hello.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
- execute
mvn package
to create an executable jar file and enterjava -jar <generated-jar-file>
refs: https://cwiki.apache.org/confluence/display/MAVEN/Tutorial%3A+Build+a+JAR+file+with+Maven+in+5+minutes https://stackoverflow.com/questions/9689793/cant-execute-jar-file-no-main-manifest-attribute https://stackoverflow.com/questions/53034953/error-source-option-5-is-no-longer-supported-use-6-or-later-on-maven-compile