0

I have a scala app with an object which implements def main(args:Array[String]): Unit trait. I use the sbtAssembly plugin to create a fat jar, and I can run it from the console using:

scala myjar.jar

However, I want to hand this over to colleagues who don't have scala installed, but only the jdk/jre. So I tried to run this with java, however it fails:

java -jar myjar.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: scala/collection/Seq
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: scala.collection.Seq
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more

What do I have to do in order to be able to run it with java? Is this even possible?

2
  • 1
    How is the assembly plugin set up? Does it exclude the Scala standard library (something like github.com/sbt/sbt-assembly#excluding-scala-library-jars)? Can you check whether scala/collection/Seq.class is in your fat jar? Commented Apr 9, 2019 at 10:18
  • Perfect! That was indeed the missing part, it was set to includeScala = false. After changing to true, I was able to run it using java -jar myjar.jar. If you add this hint as answer, I can accept it. Commented Apr 9, 2019 at 11:59

1 Answer 1

4

To run a jar using just java -jar without specifying a classpath, it must include all dependencies, including the standard Scala library (and from the error message we can see that's what's missing). By default sbt-assembly includes it, but it can be configured to exclude it and that's what your build does. Simply remove includeScala = false (or set it to true).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.