Search code examples
javagradlejarcompilationjavac

Create a Java REST Application JAR without using Gradle/Maven?


Using Gradle I know the way to create a fat jar that is ran in Spring Boot embedded Tomcat Server is:

1. Gradle clean build bootRun

I am wondering though how to create a fat jar manually using only command line in order to create and run the same exact same jar as above?

I know to create an run a very simple java application it is:

javac myApp.java
java myApp

But how to create a fat jar that contains all the projects dependencies and then run it? Without using a tool like gradle or maven?


Solution

  • Basically, you could just jar up your class(es) with a manifest that specifies where main() is. For a classic "Hello World" class named Hello, the manifest would look like this...

    Manifest-Version: 1.0
    Main-Class: Hello
    Class-Path: .
    

    You would build it like this...

    $ jar cvmf MANIFEST.MF Hello.jar Hello.class 
    added manifest
    adding: Hello.class(in = 415) (out= 285)(deflated 31%)
    

    and execute it like this...

    $ java -jar Hello.jar
    Hello World
    

    You could do the same with a more complex app with jar dependencies (including an embedded Tomcat), but you would need to unzip all the dependent jars into a directory, and include that in the jar. A build tool, like Gradle, can make that easier.