Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export Scala application to runnable JAR

Can you tell me, if this is possible, how to export a Scala application to a normal runnable JAR that can run directly in the JVM ?
Thanks

like image 986
Radu Stoenescu Avatar asked Nov 09 '11 12:11

Radu Stoenescu


2 Answers

It is perfectly possible, see for instance this: running a maven scala project. Since Scala compiles to Java bytecode, JVM is not even aware of the underlying implementation language.

In essence after compiling Scala sources using scalac you will get a bunch of .class files which you can later package into a JAR. Then you can simply run them using:

$ java -cp "your.jar:scala-library.jar" com.example.Main

Note that you must include scala-library.jar on the CLASSPATH (currently it is almost 9 MiB...) and specify class containing main method.

like image 73
Tomasz Nurkiewicz Avatar answered Oct 20 '22 18:10

Tomasz Nurkiewicz


If you use sbt to build you can use one of the one-jar plugins. They will put all dependencys into one big jar file (inclusive all the scala.jar files). This means that you only need one jar file and don't have to manage all the dependencys.

As an example with sbt-assembly (mostly copied from https://github.com/sbt/sbt-assembly):

project/plugins.sbt:

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "X.X.X")

build.sbt:

import AssemblyKeys._ // put this at the top of the file
seq(assemblySettings: _*)

then you can generate the jar with:

sbt assembly
like image 36
Fabian Avatar answered Oct 20 '22 17:10

Fabian