Vasanth Nag K V
Vasanth Nag K V

Reputation: 4988

including external jar while bulding my application jar

i am really having a rough time from past 4 days searching for methods as to how do i include the two external jars jsoup-1.7.2.jar and jxl-2.6.10.jar to my application while running it from an batch file. I have a small application that uses these jars. Yes, i searched all round the google and stackoverflow(which is my favorite!!) i tried all these links,

Setting the classpath for JAR files

How to run JAVA program through bat file

Including jar files in class path

I tried all the methods given there. but NO LUCK. So finally i taught of posting the question here! please help me. my application works fine in eclipse (where the external two jars that i have mentioned is included through build configuration. but i wanna build my application as a jar file and launch it with a .bat file!! this is the last stage of my application. and i am really wanna have a successful completion without any compramise.

Thanks in Advance!! :) :) :)

Upvotes: 1

Views: 896

Answers (2)

Zebby Dee
Zebby Dee

Reputation: 401

I use an Ant script (Ant is a build tool included with Eclipse) to package the application and all external JAR files into one executable JAR. There may be ways to make this easier (Maven?) but this has worked well for me. This is set up to work with the following project structure (you should adjust the build script as necessary if different):

build.xml
/src
  /com
     /mycompany
       /myproject
         [source files]
  /META-INF
    MANIFEST.MF
/lib
  jsoup-1.7.2.jar
  jxl-2.6.10.jar

In Eclipse:

  1. Add a build.xml file to your project root (this is recognized by Eclipse as an Ant buildfile). Here's one I use - change the "location" values near the top as necessary to point to your source root, etc. Note that the /build and /dist folders are created when the script runs (your compiled JAR will be in the /dist folder).

    <?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. If they aren't already, copy the external JAR files into the /lib folder off your project root.

  3. In your /src folder, add a folder titled META-INF. Into this folder, place a file named MANIFEST.MF that contains the following:

     Manifest-Version: 1.0
     Main-Class: [the canonical name of the class you want to launch, e.g. com.mycompany.myproject.MyApp]
    
  4. In Eclipse, right-click the build.xml file and select Run As -> Ant Build.

This will package everything up into one executable JAR file without the hassle of external classpath dependencies.

You still may want to use a batch file (or file system shortcut) to launch the JAR if you need to adjust JVM values such as min/max heap size.

Upvotes: 1

sulai
sulai

Reputation: 5354

From Eclipse, right click your Project and try:

Export... > Runnable JAR file

Choose the correct Launch configuration (the one you use to launch the Application from eclipse), and you should be set. From command line, just start your application using:

java -jar yourapp.jar [your-parameters]

Upvotes: 0

Related Questions