1

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!! :) :) :)

Community
  • 1
  • 1
Vasanth Nag K V
  • 4,860
  • 5
  • 24
  • 48

2 Answers2

1

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.

Zebby Dee
  • 401
  • 3
  • 9
  • For more in-depth info about Ant, see the documentation at Apache's Ant site: http://ant.apache.org/. – Zebby Dee Nov 12 '13 at 16:02
  • Nice, good luck! As I mentioned before, most people are using Maven as a build tool/configuration manager now rather than Ant (it's more complex than Ant but has many more features and flexibility) - if you eventually decide to check it out, there is an SO question that addresses doing this in Maven here: http://stackoverflow.com/questions/16222748/building-a-fat-jar-using-maven – Zebby Dee Nov 12 '13 at 19:26
0

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]
sulai
  • 5,204
  • 2
  • 29
  • 44
  • thanks a lot for the reply :) [your-parameters]?? please explain – Vasanth Nag K V Nov 11 '13 at 18:59
  • In case you use `args` in your `main(String[] args)`, you can pass arguments (strings) from command line to your java application using space separated words in the manner I described in the answer ;) This is optional, so if you don't use `args`, it's just `java -jar yourapp.jar`. – sulai Nov 11 '13 at 19:15
  • oh okay okay. now i did all those. but i have one line like this in my application, res= new File(getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath()); here this is returning null instead of the file location (this was working fine earlier) when i am running the java file with java -jar only this error is coming. when i run with java -cp, this error does not show up :( – Vasanth Nag K V Nov 11 '13 at 19:22