aks
aks

Reputation: 1029

How to create executable jar in scala which can run on jvm?

I am using scala version 2.11.4, I have tried various options like sbt-assembly, build artifact (Intellij Idea feature), sbt package. Unfortunately, none of them worked form me.

I attempted following things :

  1. With sbt-assembly :

Created assembly.sbt file and added following line :

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

build.sbt :

scalaVersion in ThisBuild := "2.11.4"

resolvers += Resolver.url("bintray-sbt-plugins",     url("http://dl.bintray.com/sbt/sbt-plugin-releases"))(Resolver.ivyStylePatterns)

ivyScala := ivyScala.value map { _.copy(overrideScalaVersion = true) }

I got the following error

[warn]  Note: Some unresolved dependencies have extra attributes.  Check that these dependencies exist with the requested attributes.
[warn]      com.eed3si9n:sbt-assembly:0.12.0 (sbtVersion=0.13, scalaVersion=2.11)
  1. With 'build artifact' feature using Intellij Idea 15

Able to create a jar. However, not able to execute it. Was getting following error:

Invalid or corrupt jarfile

For this I tried the command : java -jar JAR_FILE

  1. With sbt package :

Able to create JAR. However, not able to execute it. Was getting following error :

java.lang.NoClassDefFoundError scala/Function0

I was trying with the command :

java -cp scala-library.jar -jar JAR_FILE

Resolved

I am able to create jar by switching to scala version 2.10.5 and then used sbt-assembly plugin. Slightly disappointed with the fact that there is no available solution to create executable jar with latest version of scala.

Upvotes: 1

Views: 865

Answers (2)

Suma
Suma

Reputation: 34393

If you are using sbt-assembly plugin, the correct task to perform is called assembly. When using IntelliJ IDEA, sbt assembly needs to be performed from a command line, the IDE is still unable to perform such SBT tasks.

The resulting jar, which includes all dependencies, is usually called a fat jar - if you need some more searching, this is what to search for.

Upvotes: 1

ekydfejj
ekydfejj

Reputation: 339

Since I can't comment yet, I'll use answer, but more of a question - I have been using sbt package extensively for exactly this. I cd into the directory that holds src/main/scala|java and running sbt package, and then pushing jar/war file to the desired destination, in my case jetty.

Can you explain exactly what you're doing, the output and details on the issue?

Upvotes: 0

Related Questions