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.
Solution. Create a directory layout to match what SBT expects, then run sbt compile to compile your project, sbt run to run your project, and sbt package to package your project as a JAR file. Unlike Java, in Scala, the file's package name doesn't have to match the directory name.
To run scala programs with the java command, you need to include the scala library as a runtime dependency of your application. I'm including the same Compile. jar from the java example as a compile time dependency, to show how everything behaves. The scala runtime is just a jar file (scala-library.
sbt is built for Scala and Java projects. It is the build tool of choice for 93.6% of the Scala developers (2019).
In Eclipse you can do it simply as follows : Right click on your Java Project and select Export. Select Java -> Runnable JAR file -> Next. Select the Destination folder where you would like to save it and click Finish.
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+).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With