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

原文地址:https://www.cnblogs.com/mryangbo/p/11731508.html