您的位置:首页 > 编程语言 > Java开发

浅谈java 中构建可执行 jar 包的几种方式

2015-04-23 15:04 477 查看
有时候,由于项目的需要,我们会将源码编译后以工具包(class打成jar包)的形式对外提供,此时,

你的 jar 包不一定要是可执行的,只要能通过编译,能被别人以 import 的方式调用就行了。但还有的

情况是,我们的 jar 包是要可执行的,即能直接在 cmd 下直接运行。前者的打包很简单,在 eclipse 中,

直接选中要打包的 java 文件和其它资源、依赖文件, export → Java → JAR file 即可。需要注意的是,

这种方式导出的 jar 包是不可执行的,比如你执行如下的语句:

java -jar test.jar

java -classpath test.jar com.test_maven.App

会直接报错:无法找到主类或者找不到 xxx 依赖包/类,这是由于你没有定义 MANIFEST.MF 资源描述文件所致,

或者你直接把依赖的 jar 包打进了你最终的 jar,而这种嵌套的依赖 jar 包是不能直接被程序 import 识别的。

下面我们看看如何在 eclipse 中构建一个可执行的 jar 包。

方法1、最简单的还是依赖于 eclipse 的导出功能:

export → java → Runnable JAR file,这种形式的导出

可以通过 lanuch configuration 指定一个 MainClass,并会自动生成 MANIFEST.MF ,而且会帮你把依赖的 jar 包解压出来,一并打进最终

的 jar 包,这样就能被你的代码 import 引用了。





方法2、利用Eclipse的Fat Jar

上述方法是 eclipse 自带的,eclipse 也有个专门的插件叫做 Fat Jar,支持许多定制化的功能,但是这个插件有些缺陷,比如修改源码后如果你不 clean & rebuild project, 它会使用缓存重新打包,这样你的编译代码还是老的,会造成执行错误,而且这样是十分不方便的。用Fat Jar Eclipse Plug-In打包可执行jar文件

方法3、利用Maven的插件

3.1: 基于Maven构建的项目最好的方法:

将项目所支持的所有类库打在一个包中,而不是只将项目编译打包到一个jar中。这样便可以直接拷贝一个jar去执行了。

Xml代码







<plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.ebay.montage.eventprocessor.collector.CodeRollOutEventDataCollector</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>assembly</goal> </goals> </execution> </executions> </plugin>

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.ebay.montage.eventprocessor.collector.CodeRollOutEventDataCollector</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>


使用命令:

mvn assembly:assembly -DskipTests

这里就会在target目录下生成*.jar文件,

直接运行jar文件: Java -jar *.jar

备注:当然也可以使用mvn clean install -DskipTests 来构建生成jar包。



3.2: 在 maven 工程中,我们也可以很方便的打包成可执行的 jar 包。默认Maven生成的JAR包只包含了编译生成的.class文件和项目资源文件,而要得到一个可以直接在命令行通过java命令运行的JAR文件,还要满足两个条件:

JAR包中的/META-INF/MANIFEST.MF元数据文件必须包含Main-Class信息。

项目所有的依赖都必须在Classpath中,其可以通过 MANIFEST.MF 指定或者隐式设置。

Xml代码







<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.ebay.montage.eventprocessor.collector.MySpace</mainClass> </manifest> <manifestEntries> <Class-Path>.</Class-Path> </manifestEntries> </archive> <finalName>eventprocessor</finalName> <!-- <excludes> <exclude>**/*.properties</exclude> <exclude>**/*.xml</exclude> </excludes> --> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> </plugins>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.ebay.montage.eventprocessor.collector.MySpace</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>

<finalName>eventprocessor</finalName>
<!-- <excludes>
<exclude>**/*.properties</exclude>
<exclude>**/*.xml</exclude>
</excludes> -->
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>


使用mvn clean install -DskipTests打jar 包。

这样打好jar包后就可以使用:java -jar eventprocessor.jar Myspace,



当然了前提是在Myspace中有一个main方法。这样便可直接去执行Myspace中的main方法了。

但是如果要将jar包放到别的机器上去运行的话,需要将targe中的lib下的内容(上面命令中指定)也同样拷贝过去,因为默认情况下:maven只是将项目编译打包到一个jar中,而其它的类库在上面手动的指定被放到了lib中,所以在执行的时候必须要引用才行。

附:如果对Maven感兴趣,如下文章的几个功能有必要了解一下:关于 Apache Maven 您不知道的 5 件事

http://www.ibm.com/developerworks/cn/java/j-5things13/

方法4、手动维护:

当然了,如果你不怕麻烦的话,手动打包,自己建立、维护 MANIFEST.MF 文件也是可以的,我没有试过这种方法,具体可以参考如下链接:

/article/8134174.html

/article/3862791.html

转载自http://josh-persistence.iteye.com/blog/1938520
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: