18.14. Deploying a Single, Executable JAR File
Problem
You’re building a Scala application, such as a Swing application, and want to deploy a single, executable JAR file to your users.
Solution
The sbt package
command creates
a JAR file that includes the class files it compiles from your source
code, along with the resources in your project (from src/main/resources), but there are two things
it doesn’t include in the JAR file:
Your project dependencies (JAR files in your project’s lib folder, or managed dependencies declared in build.sbt).
Libraries from the Scala distribution that are needed to execute the JAR file with the
java
command.
This makes it difficult to distribute a single, executable JAR file for your application. There are three things you can do to solve this problem:
Distribute all the JAR files necessary with a script that builds the classpath and executes the JAR file with the
scala
command. This requires that Scala be installed on client systems.Distribute all the JAR files necessary (including Scala libraries) with a script that builds the classpath and executes the JAR file with the
java
command. This requires that Java is installed on client systems.Use an SBT plug-in such as sbt-assembly to build a single, complete JAR file that can be executed with a simple
java
command. This requires that Java is installed on client systems.
This solution focuses on the third approach. The first two approaches are examined in the Discussion.
Using sbt-assembly
The installation instructions ...
Get Scala Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.