Aerion
Aerion

Reputation: 73

Running Scala Jar

When I package my scala code into a jar using Eclipse, I am unable to run said jar file. I've got:

    object Hello extends App{
       println("Hello World!")
    }

and:

    public class Runner {
       public static void main(String[] args) {
          Hello.main(args);
       }
   }

I use eclipse to package this into a runnable jar called Hello.jar. java -jar Hello.jar then gives:

Exception in thread "main" java.lang.NoClassDefFoundError: scala/App
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at Hello.main(Hello.scala)
at Runner.main(Runner.java:4)
Caused by: java.lang.ClassNotFoundException: scala.App
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
... 13 more

Upvotes: 2

Views: 4983

Answers (2)

Andrzej Doyle
Andrzej Doyle

Reputation: 103787

Your application has a dependency on the Scala runtime libraries. These need to be available when the app is run, and currently they are not.

There are two ways to fix this:

  1. Specify the classpath when you launch the JAR - convenient as a one-off, but clunky. Run java -jar /path/to/yourapp.jar from the command line, and add a -cp parameter which includes the path to the Scala library JAR.
  2. Package up the Scala library JAR inside your own JAR, and modify the Manifest to reference this JAR (by adding the Class-Path attribute - see here). This requires more work with your build process, but then means that the app can be launched by simply double-clicking on the resulting JAR.

Upvotes: 5

vitalii
vitalii

Reputation: 3365

That's because eclipse don't ship scala-library with your code. Even more, you should make sure that all dependencies are in manifest file for executable jar. Here's a similar question from stackoverflow: running scala apps with java -jar

Upvotes: 1

Related Questions