21

How can I run .jar file which is generated by sbt's package?

I created a really simple example with a single .scala source:

package org.pack {
    class ScalaParser(files: Array[String]) {
        def doAll() = {
            println("hello")
        }
    }

    object Main {
        def main(args: Array[String]): Unit = {
            val sp = new ScalaParser(args)
            sp.doAll()
        }

    }
}

After running

$ sbt
> compile
> package

.jar is created in /target/scala-<version>. If I try to run it, it fails giving this error:

$ java -jar package_2.9.2-0.1.jar 
Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at org.pack.Main.main(Main.scala)
Caused by: java.lang.ClassNotFoundException: scala.ScalaObject
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 13 more

Note that no external libraries are used and sbt compile run works fine.

I attached the whole project including generated .jar on dropbox.

What is really weird is the fact that .jar Manifest contains right class to load, i.e. org.pack.Main. Maybe it is caused by something else.

System info

$ java -version
java version "1.7.0_55"
OpenJDK Runtime Environment (IcedTea 2.4.7) (7u55-2.4.7-1ubuntu1)
OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)

$ scala -version
Scala code runner version 2.9.2 -- Copyright 2002-2011, LAMP/EPFL

Additional question - what if I had some external .jars in /lib? How can I assure that they are packed? I need one .jar runnable on (possibly) every JVM.

Thanks for help.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
petrbel
  • 2,428
  • 5
  • 29
  • 49
  • You should really look into the already-available answers in the sbt tag: see Outside sbt in [tag:sbt] and http://stackoverflow.com/questions/22556499/what-are-key-differences-between-sbt-pack-and-sbt-assembly. – Jacek Laskowski Jun 16 '14 at 10:01

3 Answers3

26

You can use the sbt plugin sbt-assembly:

sbt-assembly >= 0.12.0 with sbt >= 0.13.6

Since sbt-assembly is now an auto plugin, it is sufficient to add project/assembly.sbt to your sbt project:

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

sbt-assembly 0.11

Add project/assembly.sbt to your sbt project:

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

Add assembly.sbt as well:

import AssemblyKeys._ // put this at the top of the file

assemblySettings

Usage

This gives you another sbt command:

sbt assembly

which produces a "fat jar" (which includes all dependencies, including the Scala libraries).

Now you can start your program

java -cp .../package-assembly.jar

so you only need a Java installation and the "fat jar".

Beryllium
  • 12,808
  • 10
  • 56
  • 86
  • Amazing! Thank you very much, it works increadibly simply and well. I attach github link to stb-assembly for future readers https://github.com/sbt/sbt-assembly – petrbel Jun 16 '14 at 07:44
  • 1
    @petrbel You may also like http://www.scala-sbt.org/release/docs/Community-Plugins.html. – Jacek Laskowski Jun 16 '14 at 13:44
  • 1
    Note that sbt-assembly is now an auto plugin and the way you configure it in sbt has changed since the above answer was written. For the latest settings, see: https://github.com/sbt/sbt-assembly And for migrating from old versions, see: https://github.com/sbt/sbt-assembly/blob/master/Migration.md – Brideau May 29 '17 at 21:01
  • 1
    @Brideau Thanks for your hint; I have revised the answer accordingly. – Beryllium Jun 30 '17 at 09:46
16

For those who do not wish to build a fat jar, all dependencies will have been downloaded as part of a successful run of sbt package. Classpath information is stored in a file within the target directory, which can be passed into the java command.

Example:

sbt package
java -cp target/scala-2.11/yourproject.jar:$(cat target/streams/compile/dependencyClasspath/\$global/streams/export) Main
Steven Bakhtiari
  • 3,227
  • 2
  • 20
  • 24
6

Try

scala package_2.9.2-0.1.jar

Update

From Java the command would look like this,

java -cp $SCALA_HOME/lib/scala-library.jar -jar  package_2.9.2-0.1.jar

Yet in the classpath you may need to add additional jar's, for instance -cp "jar1.jar:jar2.jar"

elm
  • 20,117
  • 14
  • 67
  • 113
  • Yes, of course, this works fine, but how can I run it on regular JVM without scala installed? I assumed that it is possible as java/scala .class files are the same but compiler info etc. Or are they not? Is it even possible? – petrbel Jun 16 '14 at 07:00
  • 1
    well you can place the scala jars in the /lib folder and when you will generate the fat jar it should package the scala libraries along with it and then you can run it where ever you want without needing to install scala – 4aRk Kn1gh7 Jun 16 '14 at 07:15
  • In case you need to mention the logger and main configuration files: java -Dlogback.configurationFile=dev-logger.xml -Dfile.ending=UTF8 -cp $SCALA_HOME/lib/scala-library.jar -jar package_2.9.2-0.1.jar -Dconfig.file=application.conf – binshi Feb 22 '18 at 07:00