Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala SBT: standalone jar

The answer: Making stand-alone jar with Simple Build Tool seems like what I need, but it did not have enough information for me, so this is a followup.

(1) How do I adapt the answer to my need? I don't understand what would need to be changed.

(2) What command do I run to create the standalone jar?

(3) Where can I find the jar after it has been created?


What I've tried:

  • Pasting the code in the linked answer verbatim into my: project/build/dsg.scala file. The file now has a

    class ForkRun(info: ProjectInfo) extends DefaultProject(info)

    (from before, used for running projects in a separate VM from SBT) and the new:

    trait AssemblyProject extends BasicScalaProject

    from the linked answer.

  • I also tried pasting the body (all defs and the lazy val of the AssemblyProject into the body of ForkRun.

To create a jar I ran package at the SBT prompt and get:

[info] Packaging ./target/scala_2.8.1/dsg_2.8.1-1.0.jar ...
[info] Packaging complete.

So I tried running the dsg_2.8.1-1.0.jar from the shell via:

java -jar dsg_2.8.1-1.0.jar 

But I get:

Failed to load Main-Class manifest attribute from
dsg_2.8.1-1.0.jar

Could this be caused by having multiple entry points into my project? I select from a list when I execute run from the SBT prompt. Perhaps I need to specify the default when creating the package?

like image 618
dsg Avatar asked May 17 '11 04:05

dsg


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.

How do I create a JAR file in Scala?

Create a jar file using 'sbt assembly' command. If you run 'sbt assembly' command before adding plugin it shows errors. 2. Distribute all the JAR files necessary with a script that builds the classpath and executes the JAR file with the scala commands.

Where are sbt JARs?

All new SBT versions (after 0.7. x ) by default put the downloaded JARS into the . ivy2 directory in your home directory. If you are using Linux, this is usually /home/<username>/.

What does sbt package do?

If you run sbt package , SBT will build a thin JAR file that only includes your project files. The thin JAR file will not include the uJson files. If you run sbt assembly , SBT will build a fat JAR file that includes both your project files and the uJson files.


2 Answers

Here's a writeup I did on one way to make an executable jar with SBT:

http://janxspirit.blogspot.com/2011/01/create-executable-scala-jar-with-sbt.html

like image 73
Janx Avatar answered Oct 26 '22 23:10

Janx


sbt-assembly is a sbt plugin to create a standalone jar of Scala sbt project with all of its dependencies.

Refer this post for more details with an example.

like image 42
prabeesh Avatar answered Oct 27 '22 00:10

prabeesh