一.环境:
1. linux
2. 有两个java类,A.java和B.java,都在同一个目录下
二.源码:
A.java
B.java
三.编译运行
四.不在同一个目录下
要提前链接各个文件(make_file(内容):com/filetool/util/A.java
com/filetool/main/B.java )
javac -source 1.7 -target 1.7 -d $APP_HOME/code/ecs/bin -encoding UTF-8 @$MAKE_FILE
二。
通过maven将应用打成jar包之后,可以通过java -jar ***.jar来执行,会运行指定的main方法,主要是 MANIFEST.MF 中指定的 main 方法;那么如果有多个main方法的时候如何运行指定的main方法哪,可以通过下面命令去执行 java -classpath ****.jar ****.****.className [args] 这里的****.****指的是包名; ****.className是类名称; args指传递进去的参数; 综上:运行jar有两种情况 1>maven打包之后通过MANIFEST.MF指定main方法 java -jar mplus-service-jar-with-dependencies.jar 如果是maven的话,可以在pom.xml中添加如下代码来指定main方法 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.3</version> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>io.test.main.MainOfDiskMount</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>assembly</goal> </goals> </execution> </executions> </plugin> </plugins> </build> 主要是这里面的mainfest代码块;mainClass中指定执行的main方法的包名+类名;然后指定maven package命令,可以看到打包出来的jar中的META-INF中MANIFEST.MF的内容添加了一项内容 这里面就是我们在pom.xml中指定的main方法;然后打包成jar运行的时候,如果要添加参数,直接添加在后面就可以了; java -jar mplus-service-jar-with-dependencies.jar aaa bbb 2>如果没有在MANIFEST.MF中指定,jar中有多个main方法,指定运行某个特定的main方法的命令 java -cp mplus-service-jar-with-dependencies.jar com.smbea.dubbo.bin.Console aaa bbb 然后再main方法中添加打印的逻辑,来看下执行的参数情况 其实就是把aaa,bbb的值作为args参数传递进去了; 总结:通过maven 把项目打包成jar,然后运行main方法,有两种情况,一种是在MANIFEST.MF中指定运行的main方法;另外一种是通过命令行指定运行的main方法的包;同时可以添加对应的参数传递到main方法中去;
原文:https://www.cnblogs.com/SunshineKimi/p/11306519.html