我使用 Ant 脚本(Ant 是 Eclipse 中包含的构建工具)将应用程序和所有外部 JAR 文件打包到一个可执行 JAR 中。可能有办法让这更容易(Maven?)但这对我来说效果很好。这设置为使用以下项目结构(如果不同,您应该根据需要调整构建脚本):
build.xml
/src
/com
/mycompany
/myproject
[source files]
/META-INF
MANIFEST.MF
/lib
jsoup-1.7.2.jar
jxl-2.6.10.jar
在 Eclipse 中:
将 build.xml 文件添加到项目根目录(Eclipse 将其识别为 Ant 构建文件)。这是我使用的一个 - 根据需要更改顶部附近的“位置”值以指向您的源根等。请注意 /build 和 /dist 文件夹是在脚本运行时创建的(您编译的 JAR 将在 /dist文件夹)。
<?xml version="1.0" encoding="UTF-8"?> <!-- XML file header -->
<!-- Define the Ant project name and default task. The project name goes into a
variable named "ant.project.name". The default task is the one that will be
run automatically by choosing Run As -> Ant Build from the context menu in
Eclipse -->
<project name="MyProject" default="package">
<!-- Set variables for folder paths that will be used later in the script.
The "name" attribute defines the name of the variable (e.g source.dir).
The "location" attribute is the relative path of the folder (from the
project root). -->
<property name="source.dir" location="src"/>
<property name="bin.dir" location="bin"/>
<property name="lib.dir" location="lib"/>
<property name="build.dir" location="build"/>
<property name="dist.dir" location="dist"/>
<!-- Define the "package" task, which depends on the "clean" task (meaning
"clean" will be run automatically when "package" is invoked). -->
<target name="package" depends="clean" description="Remake the jarfile from scratch">
<!-- Make a folder with the name in the build.dir variable ("/build") -->
<mkdir dir="${build.dir}" />
<!-- Make the "/dist" folder, into which the compiled JAR will go -->
<mkdir dir="${dist.dir}" />
<!-- Unzip any JAR files from the "/lib" folder into "/build" -->
<unzip dest="${build.dir}">
<fileset dir="${lib.dir}" includes="*.jar" />
</unzip>
<!-- Copy everything from "/bin" to "/build" -->
<copy todir="build">
<fileset dir="${bin.dir}" includes="**/*.*" />
</copy>
<!-- Set the location of the JAR manifest in the "manifest.mf"
variable. -->
<property name="manifest.mf" location="${build.dir}/META-INF/MANIFEST.MF"/>
<!-- Create a JAR file from everything in "/build". Set the JAR
manifest to the file at the location contained in the
"manifest.mf" variable. The completed JAR will be located in
the "/dist" folder and will be named "MyProject.jar". -->
<jar destfile="${dist.dir}/${ant.project.name}.jar" duplicate="preserve" manifest="${manifest.mf}">
<fileset dir="${build.dir}"/>
</jar>
<!-- Delete the "/build" folder -->
<delete dir="${build.dir}"/>
</target>
<!-- Define the "clean" task, which deletes any old "/build"
and "/dist" folders -->
<target name="clean" description="Delete the working build and distribution folders">
<!-- Delete the "/build" folder -->
<delete dir="${build.dir}"/>
<!-- Delete the "/dist" folder -->
<delete dir="${dist.dir}"/>
</target>
</project>
如果还没有,请将外部 JAR 文件复制到项目根目录下的 /lib 文件夹中。
在您的 /src 文件夹中,添加一个名为 META-INF 的文件夹。在此文件夹中,放置一个名为 MANIFEST.MF 的文件,其中包含以下内容:
Manifest-Version: 1.0
Main-Class: [the canonical name of the class you want to launch, e.g. com.mycompany.myproject.MyApp]
在 Eclipse 中,右键单击 build.xml 文件并选择 Run As -> Ant Build。
这会将所有内容打包到一个可执行的 JAR 文件中,而没有外部类路径依赖项的麻烦。
如果您需要调整 JVM 值(例如最小/最大堆大小),您仍可能希望使用批处理文件(或文件系统快捷方式)来启动 JAR。