1

从过去的 4 天开始,我真的很难找到关于如何在从批处理文件运行应用程序时将两个外部 jars jsoup-1.7.2.jar 和 jxl-2.6.10.jar 包含到我的应用程序的方法。我有一个使用这些罐子的小应用程序。是的,我在 google 和 stackoverflow 上进行了搜索(这是我最喜欢的!!)我尝试了所有这些链接,

设置 JAR 文件的类路径

如何通过bat文件运行JAVA程序

在类路径中包含 jar 文件

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

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

4

2 回答 2

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

于 2013-11-11T20:16:49.463 回答
0

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

Export... > Runnable JAR file

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

java -jar yourapp.jar [your-parameters]
于 2013-11-11T18:43:40.093 回答