SBT project for java executable jar
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+).
Related videos on Youtube
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, 2020Comments
-
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 about 13 yearsHave 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 about 13 yearsThanks 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 about 13 yearsThanks Thomas. It's a good one. Simple to use. Would benefit from having proguard trim out the fat.
-
Philippe over 12 yearsYou can use the Proguard plugin to do that. Make sure you specify that you want to keep the main class.
-
Thomas Lockney over 12 yearsYes, 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 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?