To create executable JAR we need to create new artifact and build it. Build will save everything to the output folder.
The version of java used in the article was 1.8.
Automatically created build artifact:
- Open File/Project structure
- Create new artifact, JAR, from modules with dependencies:
- Enter proper startup class and select proper function how to use external 3rd party libraries in the target artifact (“copy to the output directory and link via manifest“). We don’t want to extract them to the target JAR file because some of them are probably signed and those will not work inside the target JAR.
Now you can build artifact from the menu (/Build/Build artifact/) and test it from the command line:
$ java -jar out/artifacts/sparkweb/sparkweb.jar
Customized way of defining build artifact
If you wish to have more influence on target artifact then you use “Other” artifact type and design the process manually:
You can move external libraries into some sub folder for example and then create manifest file manually to reflect that new class path.
Sample manifest file:
If you edit MANIFEST.MF file just don’t forget to put two spaces between the jar files.
Read this blog for more detailed instructions..
Why we don’t create a single JAR from everything
Create one single JAR from everything the application consist is not a good idea because of security limitations. If any of 3rd party jar libraries is signed then the signature represent physical JAR file and when code is loaded from another jar file (your single jar) the load mechanism will prevent loading because the signature doesn’t represent the jar file anymore.