一般状况下,一个java项目中,主程序只有一个,这种状况下,打包jar时,通常须要指定主程序。拿maven来讲,配置主程序的方式以下:html
添加pox.xmljava
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>cn.vicky.skstatistics.money.App </mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> <finalName>SkMoneyStatistics</finalName> </build>
能够经过双击方式运行jar程序。
若是一个jar中含有多个主程序,而你没有配置默认主程序,或者想要运行指定主程序,则能够经过以下命令执行:apache
Java -classpath example03-1.0-SNAPSHOT.jar cn.vicky.example03.HelloAgentmaven
classpath(简写-cp) <目录和 zip/jar 文件的类搜索路径>ui
example03-1.0-SNAPSHOT.jar jar包spa
cn.vicky.example03.HelloAgent 指定的主程序类.net