paintcan
paintcan

Reputation: 703

running scala apps with java -jar

I got some problems with the java. Check it out.

sebastian@sebastian-desktop:~/scaaaaaaaaala$ java -cp /home/sebastian/.m2/repository/org/scala-lang/scala-library/2.8.0.RC3/scala-library-2.8.0.RC3.jar:target/scaaaaaaaaala-1.0.jar scaaalaaa.App

Hello World!

That's cool, right, but how bout this:

sebastian@sebastian-desktop:~/scaaaaaaaaala$ java -cp /home/sebastian/.m2/repository/org/scala-lang/scala-library/2.8.0.RC3/scala-library-2.8.0.RC3.jar -jar target/scaaaaaaaaala-1.0.jar

Exception in thread "main" java.lang.NoClassDefFoundError: scala/Application
 at java.lang.ClassLoader.defineClass1(Native Method)
 at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
 at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
 at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
 at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
 at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
 at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
 at scaaalaaa.App.main(App.scala)
Caused by: java.lang.ClassNotFoundException: scala.Application
 at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
 ... 13 more

Wat the heck? Any idea why the first works and not the 2nd? How do I -jar my scala??

My thanks in advance, brother.

Upvotes: 40

Views: 64349

Answers (5)

Hartmut Pfarr
Hartmut Pfarr

Reputation: 6139

use sbt-assembly sbt plugin to produce a jar containing all dependencies

sbt-assembly github

e.g. add a line in project/plugins.sbt:

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

adjust your build.sbt e.g. with

mainClass in (assembly) := Some("mypackage.mysubpackage.MyMainClass")
assemblyJarName in assembly := "my_assembly.jar"

then build your assembled jar with

sbt assembly

and launch it with

java -jar ./target/scala-2.12/my_assembly.jar

Et voilà, no class-path this & thats needed any more. only 1 jar.

Upvotes: 6

Nilesh Shinde
Nilesh Shinde

Reputation: 469

Below is the way to execute the Scala Executable Jar

  • We need scala installed in your system.

  • Here first will give the jar path and jar name

  • If your code needs any dependency jar then keep all jar in one directory and give a path of this in command like below after the ":".

  • At last give your class/object name.

scala -cp "/your/jar/path/jar_name.jar:/your/dependency/jar/path/*" SampleCode

Upvotes: 0

hgrey
hgrey

Reputation: 3093

Simply running

scala scaaaaaaaaala-1.0.jar 

works for me with 2.11.6

Upvotes: 6

Thomas Jung
Thomas Jung

Reputation: 33092

If you define -jar the -classpath is ignored:

Java manual:

-jar When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

You can define the classpath dependencies in the Manifest metadata.

The easiest way to start your app is using the scala scripts:

scala -classpath target/scaaaaaaaaala-1.0.jar scaaalaaa.App Hello World!

Upvotes: 51

Don Roby
Don Roby

Reputation: 41137

For an executable jar, the classpath should be in the jar's manifest. For help on doing that, look through the answers to Stackoverflow: How to create an executable jar with dependancy jars.

Upvotes: 4

Related Questions