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

时间:2013-11-11 18:35:50

标签: java eclipse batch-file jar

我真的有一段艰难的时间从过去的4天中搜索方法,我如何将两个外部jars jsoup-1.7.2.jar和jxl-2.6.10.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文件启动它!!这是我的应用程序的最后一个阶段我真的希望在没有任何竞争的情况下顺利完成。

先谢谢!! :) :) :))

2 个答案:

答案 0 :(得分: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构建文件)。这是我用的一个 - 改变了 “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. 如果尚未将它们复制到项目根目录下的/ 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 - &gt; Ant Build。

  5. 这会将所有内容打包成一个可执行的JAR文件,而不会有外部类路径依赖的麻烦。

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

答案 1 :(得分:0)

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

Export... > Runnable JAR file

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

java -jar yourapp.jar [your-parameters]
相关问题