Eclipse 打包項目jar 及依賴其他jar包


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM