tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Articles > Java > Basics > How to create Java Executable JAR with JAR dependencies

How to create Java Executable JAR with JAR dependencies 

The following example shows creating an executable JAR file with other JAR dependencies. First create a simple standalone java application, for instance a simple Swing UI application with two dependent libraries (commons-lang3-3.0.1.jar and commons-io-2.1.jar).


File Name  :  
/JavaExeWithLibsTest/ExeJARApplication.java 
   
package com.btc;

import javax.swing.JButton;
import javax.swing.JFrame;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomStringUtils;

public class ExeJARApplication {

  public static void main (String [] argsthrows Exception {
    
    //Get Temp directory size in bytes
    long dirSize = FileUtils.sizeOfDirectory(new File("C:/Temp"));
  
    String htmlButton = "<html>Random String : " + RandomStringUtils.randomAlphabetic(6"<br>" +
              "Temp directory size : " + FileUtils.byteCountToDisplaySize(dirSize"</html>";
    
    JFrame frame = new JFrame("My Application");
    frame.getContentPane().add(new JButton(htmlButton));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    frame.setSize(200200);
    frame.setLocation(24090);
    frame.setVisible(true);
  }
}
   

Compile and run the application for sanity.

javac -cp lib/commons-lang3-3.0.1.jar;lib/commons-io-2.1.jar -d . ExeJARApplication.java
java -cp lib/commons-lang3-3.0.1.jar;lib/commons-io-2.1.jar;. com.btc.ExeJARApplication
Create a manifest file manifest.txt with Main-Class attribute. Ensure to enter a new line at the end of manifest file. Otherwise some systems may throw failed to load Main-Class error.
Main-Class: com.btc.ExeJARApplication
Class-Path: lib/commons-lang3-3.0.1.jar lib/commons-io-2.1.jar 

Create an executable JAR using jar command from the console (Ensure to include JDK_HOME\bin in Path environment variable).
jar cvfm [jar-file] [manifest-file] [dirs/files]
jar cvfm test.jar manifest.txt com lib
Run the executable JAR from the console or from "Open with" windows dialog.
java -jar test.jar
It gives the following output,




 
  


  
bl  br