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,648
  • 5
  • 23
  • 42
  • An alternate way which I've found to be neater than referencing external JARs is to package up the application and all external dependencies into one executable JAR. This involves unpacking the contents of the external JAR files into your build folder, then JAR'ing the whole thing up. There are 3rd party applications that will do this for you (e.g. Launch4j) but I've found a simple Ant script does the trick. – Zebby Dee Nov 11 '13 at 19:07
  • oh this sounds nice and clean, may i know how to do this?? you mentioned abt Ant script, may i know how to use it and what is there in that script??? Please :) – Vasanth Nag K V Nov 11 '13 at 19:14
  • I will add it as an answer... gimme a minute. – Zebby Dee Nov 11 '13 at 19:31
  • yeah okay thanks a lot! please do tell me how ti use it. m not sure what an ant script is.. – Vasanth Nag K V Nov 11 '13 at 19:46

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
  • are these the things that gets built in to a jar??? i mean, here i should mention wat all needs to be packaged?? – Vasanth Nag K V Nov 12 '13 at 08:47
  • And one more thing, in the property tag there is no attribute like "value" . Please explain the ant script here. it will be like a bonus knowledge here for me. thanks a lot for taking time to post the ant script here. :) – Vasanth Nag K V Nov 12 '13 at 11:25
  • Ok, I've added comments to the Ant script to explain what's happening. You set location variables in Ant with `` and use them later in the script as `"${[name]}"`. I realized when editing that there's some fluff (for example, you might find it simpler to just hardcode the path names rather than using Ant variables, and the "clean" task could be rolled into the "package" task) but I took this snippet from a much larger Ant buildfile with many tasks, so it made sense to use the variables rather than typing path names over and over. – Zebby Dee Nov 12 '13 at 15:59
  • 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
  • awesome!! :) but now i have a small doubt before i start off. i am not able to get my external jars into the lib folder as you have shown in your folder architecture. why is this happening?? how do i get it to lib folder?? – Vasanth Nag K V Nov 12 '13 at 18:59
  • i figured it out!! :) – Vasanth Nag K V Nov 12 '13 at 19:11
  • thanks a lot Zebby!!i finally did it!! no no.. you made me do it!!! thank you so much!!!! my project Complete!!! all credits to you!! learnt some beautiful things all round this project!! :) :) :) :) – Vasanth Nag K V Nov 12 '13 at 19:24
  • 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
  • Also, wouldn't mind an "Accept" if you found my answer helpful and are so inclined... ;) – Zebby Dee Nov 12 '13 at 19:28
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
  • 4,966
  • 2
  • 27
  • 41
  • 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