Create a folder Haha, which contains three items
- Haha.java
- Main.java
- Pom.xml
Pom.xml
<?xmlVersion= "1.0" encoding= "UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Wyf</groupId> <artifactId>Clonesite</artifactId> <version>1.0-snapshot</version> <build> <sourceDirectory>.</sourceDirectory> <finalName>haha</finalName> <plugins> <plugin> <groupId>Org.apache.maven.plugins</groupId> <artifactId>Maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>Utf-8</encoding> </configuration> </plugin> </plugins> </build></project>
The MAVEN compliance convention is more than the configuration principle, it has a default directory structure, but you can also specify the source code directory and the resource file directory in build.
Main.java
publicclass Main{ publicstaticvoidmain(String[] args) { System.out.println("天下大势为我所控"); }}
Haha.java
publicclass Haha { publicstaticvoidmain(String[] args) { System.out.println("hello world"); }}
Open the CMD,MVN package under this folder and pack to generate the target directory.
CD to target, execution java -cp haha.jar Main
runs the main class, and execution java -cp haha.jar Haha
runs the Haha class.
I think this way of running the jar is better than packaging it directly into a runnable jar package because it's easier to package.
The executable jar package can actually java -cp haha.jar Something
run other classes in the same way, except that it has the default main class.
Maven Packaged executable jar package