java - 在构建我的应用程序 jar 时包括外部 jar

标签 java eclipse batch-file jar

在过去的 4 天里,我确实经历了一段艰难的时间,寻找如何在从批处理文件运行应用程序时将两个外部 jar jsoup-1.7.2.jar 和 jxl-2.6.10.jar 包含到我的应用程序中的方法。我有一个使用这些 jar 的小应用程序。是的,我搜索了所有谷歌和 stackoverflow(这是我最喜欢的!!)我尝试了所有这些链接,

Setting the classpath for JAR files

How to run JAVA program through bat file

Including jar files in class path

我尝试了那里给出的所有方法。但运气不好。所以最后我教了在这里发布问题!请帮我。我的应用程序在 Eclipse 中运行良好(其中我提到的外部两个 jar 是通过构建配置包含的。但我想将我的应用程序构建为 jar 文件并使用 .bat 文件启动它!!这是我的应用程序的最后阶段。我真的希望成功完成,没有任何妥协。

提前致谢!! :) :) :)

最佳答案

我使用 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 中:

  1. 将 build.xml 文件添加到项目根目录(这是由 Eclipse 作为 Ant 构建文件)。这是我使用的 - 更改 根据需要,靠近顶部的“location”值指向您的源根目录等。请注意,脚本运行时会创建/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>
    
  2. 如果尚未将外部 JAR 文件复制到项目根目录下的/lib 文件夹中。

  3. 在/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]
    
  4. 在 Eclipse 中,右键单击 build.xml 文件并选择 Run As -> Ant Build。

这会将所有内容打包到一个可执行 JAR 文件中,而无需外部类路径依赖项的麻烦。

如果您需要调整 JVM 值(例如最小/最大堆大小),您可能仍然需要使用批处理文件(或文件系统快捷方式)来启动 JAR。

关于java - 在构建我的应用程序 jar 时包括外部 jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19913384/

相关文章:

java - 如何在 cpanel linux 托管的 wordpress 网站上发布一个简单的 Java 数学计算应用程序?

java - 如何在 Angular 7 中使用字符串添加动态查询参数?

android - 如何在首选项中列出 GNU C

android - Eclipse 在文件中显示没有错误,但在文件夹中

batch-file - 如何创建批处理文件计时器以全天执行/调用另一个批处理

batch-file - 从 BIOS 获取 Windows 产品 key

java - 长轮询 杀死服务器端的线程

java - 创建和使用数据库 : Android

eclipse - 如何在 Eclipse IDE 中使用 Git 工作树?

batch-file - 使用批处理文件打开vscode时,cmd打开不关闭