Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SBT project for java executable jar

Tags:

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.

like image 486
Synesso Avatar asked May 03 '11 01:05

Synesso


People also ask

How do I run a JAR file in sbt?

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.

Can Java JAR be used in Scala?

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.

Can sbt be used for Java?

sbt is built for Scala and Java projects. It is the build tool of choice for 93.6% of the Scala developers (2019).

How do I create an executable Java JAR file?

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.


1 Answers

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+).

like image 87
Thomas Lockney Avatar answered Sep 27 '22 16:09

Thomas Lockney