How to create scala jar using sbt for beginner

This article will share the following contents.

  • Prepare environment
  • How to create scala jar
  • How to create Fat jar using sbt-assembly plugin

※This article can give you how to use sbt a little bit, but you should learn about sbt from the official documentation if you’d like to learn about it well.

Prepare environment

You need to prepare an environment with sbt and JDK installed.

Docker will allow you to quickly prepare environment.

  • Create new directory (mkdir sbt_test) and cd into sbt_test
  • Create docker-compose.yml (docker image link)
  version: '3'
      image: hseeberger/scala-sbt:8u222_1.3.5_2.13.1
      container_name: sbt_test
      tty: true
      volumes: [".:/work"]
  • Execute docker-compose up -d command
  • Enter in docker container with docker exec -it sbt_test /bin/bash and cd into /work
  user@linux:~/sbt_test$ docker exec -it sbt_test /bin/bash
  root@cd80666fdbe1:~# cd /work
  • Check JDK version with java -version
  root@cd80666fdbe1:/work# java -version
  openjdk version "1.8.0_222"
  OpenJDK Runtime Environment (build 1.8.0_222-b10)
  OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)

You can follow this document (Installing sbt) if you want to use local PC environment.

How to create scala jar

Create the project

  • Run the following command. This pulls the ‘hello-world’ template from GitHub.
  sbt new scala/hello-world.g8
  • When prompted, name the application “hello-world”
  A template to demonstrate a minimal Scala application 

  name [Hello World template]: hello-world

  Template applied in /work/./hello-world
  • cd into hello-world
    Directory tree structure
  - hello-world
    - project
    - src
        - main
            - scala
                - Main.scala
    - build.sbt

Prepare files

  • src/main/scala/Main.scala (Scala source file)
  object Main {
    def main(args: Array[String]) {
      println("Hello, World!")
  • build.sbt (build definition file for sbt)
  name := "hello-world"
  organization := "gotoqcode.scala"
  version := "1.0"

  scalaVersion := "2.11.12"
  libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "2.1.1"

You can use build.sbt domain-specific language(DSL)

  • project/ (settings for sbt)

Run the project

The first time you run sbt, it may take a while to download all the dependencies it needs, but after that first run, it’ll download new dependencies only as needed.

  • Run the project
  sbt run

Create jar file

  • Package the project as a jar file
  sbt package

jar file can be created under target/scala-2.11/ directory.

Execute jar file

  • Execute with scala interpreter
  scala target/scala-2.11/hello-world_2.11-1.0.jar 
  • Execute with java interpreter
  java -cp target/scala-2.11/hello-world_2.11-1.0.jar:$(cat target/streams/compile/dependencyClasspath/_global/streams/export) Main

cat target/streams/compile/dependencyClasspath/_global/streams/export means dependencies.
* /root/.cache/coursier/v1/https/
* /root/.cache/coursier/v1/https/

You have to add all dependencies as class path because this jar file(target/scala-2.11/hello-world_2.11-1.0.jar) doesn’t contain any dependencies. You can list its contents with the jar tvf command. You can check that this jar file has only Manifest file and program(Main class file).

  root@cd80666fdbe1:/work/test# jar tvf target/scala-2.11/hello-world_2.11-1.0.jar
     285 Fri Jan 01 00:00:00 UTC 2010 META-INF/MANIFEST.MF
     661 Fri Jan 01 00:00:00 UTC 2010 Main$.class
     668 Fri Jan 01 00:00:00 UTC 2010 Main.class

How to create Fat jar using sbt-assembly plugin

What is Fat jar

A Fat jar also know as a uber jar or jar with dependencies is a jar file that contains not only program, but embeds its dependencies as well.

You can execute Fat jar in the following command, without classpath option.

java -jar <Fat _jar_'s path>

How to use sbt-assembly plugin

You can create a Fat jar using sbt-assembly plugin.

  • Prepare project/plugins.sbt to use sbt-assembly plugin
  addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "1.2.0")
  • Run sbt assembly command
  • Execute with java interpreter
  java -jar target/scala-2.11/hello-world-assembly-1.0.jar

You can confirm that Fat jar file contains all dependencies using the following command.

  jar tvf target/scala-2.11/hello-world-assembly-1.0.jar

< References >