SBT project for java executable jar

11,367

Use the assembly-sbt plugin (originally created by Coda Hale, now maintained by Eugene Yokota): https://github.com/sbt/sbt-assembly

Once you add this to your project it will build a so-called "fatjar" which is executable via java -jar projectName-assembly.jar. It will autodetect your main method -- if there is more than one in your source, you can explicitly set which one to use by setting mainclass, e.g.:

mainClass in assembly := Some("com.package.ClassNameWithMain")

Note: I edited this answer to be up-to-date with the current release of SBT (0.11+).

Share:
11,367

Related videos on Youtube

Synesso
Author by

Synesso

I first started programming BASIC and rudimentary machine language on my Commodore 16 in the 1980s. These days I work remotely from the beautiful Gold Coast in several different languages - mainly Scala, Rust & Go. My passions are Jazz, Coffee and learning 日本語. If I have been helpful, please thank me by buying a coffee for a stranger.

Updated on August 20, 2020

Comments

  • Synesso
    Synesso over 3 years

    What is the best way to set the target package for an SBT project to be an executable jar file?

    It would need to bundle scala-library.jar and set the manifest for main-method.

    • huynhjl
      huynhjl about 13 years
      Have a look at github.com/retronym/sbt-onejar. I haven't tried myself but I'm interested to know if that works. Also if that does not work, retronym also points to github.com/nuttycom/sbt-proguard-plugin.
    • Synesso
      Synesso about 13 years
      Thanks huynhjl. It worked fine, but I decided against it as one-jar itself seems full of classloading jiggery-pokery. I prefer the mega-jar approach of assembly-sbt.
  • Synesso
    Synesso about 13 years
    Thanks Thomas. It's a good one. Simple to use. Would benefit from having proguard trim out the fat.
  • Philippe
    Philippe over 12 years
    You can use the Proguard plugin to do that. Make sure you specify that you want to keep the main class.
  • Thomas Lockney
    Thomas Lockney over 12 years
    Yes, Proguard can do this, but it's a pain to configure correctly. The beauty of the sbt-assembly is how incredibly simple it is.
  • Nishan
    Nishan over 7 years
    @folone I tried this using assembly and generated jar but still it won't execute using double click. How do I create executable jar which will run when you click on it?