【问题标题】:including external jar while bulding my application jar在构建我的应用程序 jar 时包括外部 jar
【发布时间】:2013-11-11 18:35:49
【问题描述】:

从过去的 4 天开始,我真的很难找到有关如何在从批处理文件。我有一个使用这些罐子的小应用程序。是的,我在 google 和 stackoverflow 上进行了全面搜索(这是我最喜欢的!!)我尝试了所有这些链接,

Setting the classpath for JAR files

How to run JAVA program through bat file

Including jar files in class path

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

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

【问题讨论】:

  • 我发现比引用外部 JAR 更简洁的另一种方法是将应用程序和所有外部依赖项打包到一个可执行 JAR 中。这涉及将外部 JAR 文件的内容解压缩到您的构建文件夹中,然后将整个内容进行 JAR 处理。有 3rd 方应用程序可以为您执行此操作(例如 Launch4j),但我发现一个简单的 Ant 脚本可以解决问题。
  • 哦,这听起来不错,很干净,我可以知道怎么做吗?您提到了 abt Ant 脚本,我可以知道如何使用它以及该脚本中有什么吗???请:)
  • 我会添加它作为答案...给我一分钟。
  • 好的,非常感谢!请告诉我如何使用它。我不确定什么是 ant 脚本..

标签: java eclipse batch-file jar


【解决方案1】:

我使用 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 构建文件)。这是我使用的一个 - 改变 顶部附近的“位置”值需要指向您的源根等

    <?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。

【讨论】:

  • 这些是内置到 jar 中的东西吗? ??我的意思是,在这里我应该提到所有需要打包的东西??
  • 还有一件事,在属性标签中没有像 "value" 这样的属性。请在此处解释 ant 脚本。这对我来说就像一个额外的知识。非常感谢您花时间在此处发布 ant 脚本。 :)
  • 好的,我已经在 Ant 脚本中添加了 cmets 来解释发生了什么。您在 Ant 中使用&lt;property name="[name]" location="[value]"/&gt; 设置位置变量,稍后在脚本中将它们用作"${[name]}"。我在编辑时意识到有一些绒毛(例如,您可能会发现硬编码路径名而不是使用 Ant 变量更简单,并且“干净”任务可以滚动到“包”任务中)但我接受了这个 sn -p 来自一个包含许多任务的更大的 Ant 构建文件,因此使用变量而不是一遍又一遍地输入路径名是有意义的。
  • 有关 Ant 的更多详细信息,请参阅 Apache 的 Ant 站点上的文档:ant.apache.org
  • 太棒了!! :) 但现在在我开始之前我有一个小小的疑问。正如您在文件夹架构中显示的那样,我无法将我的外部 jar 放入 lib 文件夹。为什么会这样??我如何将它放到 lib 文件夹中??
【解决方案2】:

在 Eclipse 中,右键单击您的项目并尝试:

Export... > Runnable JAR file

选择正确的启动配置(用于从 Eclipse 启动应用程序的配置),您应该已设置好。从命令行,只需使用以下命令启动您的应用程序:

java -jar yourapp.jar [your-parameters]

【讨论】:

  • 非常感谢您的回复 :) [your-parameters]??请解释
  • 如果您在main(String[] args) 中使用args,您可以按照我在答案中描述的方式,使用空格分隔的单词将参数(字符串)从命令行传递到您的Java 应用程序;)这个是可选的,所以如果你不使用args,它只是java -jar yourapp.jar
  • 哦好吧好吧。现在我做了所有这些。但我的应用程序中有这样一行, res= new File(getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath());当我使用 java -jar 运行 java 文件时,这里返回的是 null 而不是文件位置(之前工作正常),只有这个错误来了。当我使用 java -cp 运行时,不会出现此错误:(
猜你喜欢
  • 1970-01-01
  • 2017-07-31
  • 2018-09-24
  • 2015-06-01
  • 1970-01-01
  • 2013-11-07
  • 2011-04-15
  • 2012-09-13
相关资源
最近更新 更多