Andriy has more than 12 years of software development experience as Programmer/Software Developer/Senior Software Developer/Team Lead/Consultant, working extensively with Microsoft .NET, Sun JEE and Adobe Flex platforms using Java, C#, C++, Groovy, Scala, Ruby, Action Script, Grails, MySQL, PostreSQL, MongoDB, and more. Andriy is a DZone MVB and is not an employee of DZone and has posted 25 posts at DZone. You can read more from them at their website. View Full User Profile

Simple but Powerful Concept: Packing Your Java Application as One JAR

  • submit to reddit


Today's post will target an interesting but quite powerful concept: packing your application as single, runnable JAR file, also known as one or fat JAR.

We get used to large WAR archives which contain all dependencies packed together under some common folder structure. With JAR-like packaging the story is a bit different: in order to make your application runnable (via java -jar) all dependencies should be provided over classpath parameter or environment variable. Usually it means there would be some lib folder with all dependencies and some runnable script which will do the job to construct classpath and run JVM. Maven Assembly plugin is well know for making such kind of application distribution.

A slightly different approach would be to package all your application dependencies to the same JAR file and make it runnable without any additional parameters or scripting required. Sounds great but ... it won't work unless you add some magic: meet One-JAR project.

Let's briefly outline the problem: we are writing a stand-alone Spring application which should be runnable just by typing java -jar <our-app.jar>.

As always, let's start with our POM file, which will be pretty simple

<project xmlns="" xmlns:xsi="" xsi:schemalocation="">







Our sample application will bootstrap Spring context, get some bean instance and call a method on it. Our bean is called SimpleBean and looks like:

package com.example;

public class SimpleBean {
    public void print() {
        System.out.println( "Called from single JAR!" );

Falling in love with Spring Java configuration, let us define our context as annotated AppConfig POJO:

package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.example.SimpleBean;

public class AppConfig {
    public SimpleBean simpleBean() {
        return new SimpleBean();

And finally, our application Starter with main():

package com.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.example.config.AppConfig;

public class Starter {
    public static void main( final String[] args ) {
        ApplicationContext context = new AnnotationConfigApplicationContext( AppConfig.class );
        SimpleBean bean = context.getBean( SimpleBean.class );

Adding our main class to META-INF/MANIFEST.MF allows to leverage Java capabilities to run JAR file without explicitly specifying class with main() method. Maven JAR plugin can help us with that.


Trying to run java -jar spring-one-jar-0.0.1-SNAPSHOT.jar will print the exception to the console: java.lang.NoClassDefFoundError. The reason is pretty straightforward: even such a simple application as this one already required following libraries to be in classpath.


Let's see what One-JAR can do for us here. Thanks to availability of onejar-maven-plugin we can add one to the plugins section of our POM file.


Also, pluginRepositories section should contain this repository in order to download the plugin.


As the result, there will be another artifact available in the target folder, postfixed with one-jar: Running this one with java -jar will print to the console:

Called from single JAR!

Fully runnable Java application as single, redistributable JAR file! The last comment: though our application looks pretty simple, One-JAR works perfectly for complex, large applications as well without any issues. Please, add it to your toolbox, it's really useful tool to have.

Thanks to One-JAR guys!

Published at DZone with permission of Andriy Redko, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Emmanuel Bourg replied on Thu, 2012/11/08 - 4:12am

 The Maven Shade Plugin is also worth mentioning, it's able to minimize the jar by keeping only the classed used in the dependencies.

Raging Infernoz replied on Fri, 2012/11/09 - 7:06am

Shade renders On-JAR pretty much obsolete; On-JAR was only needed because Ant was total pain-in-the-butt useless at building composite jars, whereas Shade is amazing and allows you to pick which Maven coordinates to include/exclude and which files to include/exclude, so you can mix jars how you like, and setting Manifest entries and other meta-data is easy too; all this without any classloader hacking and the fragility this will cause, eventually.

Ed Staub replied on Fri, 2012/11/09 - 9:27am

What, if any, are the licensing considerations and best practices when doing this with third-party open-source software?

Abhijeet Sutar replied on Fri, 2012/11/16 - 3:23am

I was searching this kind tool for packing standalone application. Thanks !

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.