1.使用eclipse 導出testsolr.jar包,被導項目依賴其他的jar包,這些jar包信息要寫到MANIFEST.MF文件中。在運行testsolr.jar時,需要在testsolr.jar同目錄下放好需要的jar包。
編輯MANIFEST.MF文件:
指定main方法:
使用MANIFEST.MF中配置的Main-Class 啟動:
使用該種方式運行jar包必須在jar包外添加需要用的jar,運行時依賴。
2.使用maven打包,將項目及其依賴的jar打到一個jar中。
新建成maven,在pom.xml中添加<build>
<build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId> maven-assembly-plugin </artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>solr.main.SolrAction</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
使用maven打包:maven clean,maven install。在target生成帶依賴的jar包:
jar內結構:
META-INF:
再執行就不需要在jar包配置依賴的jar了。java -jar solrtest.jar arg0 arg1 (會直接運行pom中配置的mainclass)。
使用java -cp solrtest.jar xxx.xxx.Main arg0 arg1 (指定運行jar中的任一main方法)
=========================================
參考地址:https://www.cnblogs.com/f-zhao/p/6929814.html