40

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.

Fabian N.
  • 3,807
  • 2
  • 23
  • 46
paintcan
  • 703
  • 1
  • 6
  • 10

5 Answers5

50

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!
Michał Kuliński
  • 1,928
  • 3
  • 27
  • 51
Thomas Jung
  • 32,428
  • 9
  • 84
  • 114
  • 14
    I would prefer a personal Descartes or Newton. :-) – Thomas Jung May 28 '10 at 15:38
  • 1
    This is good. But I think the motivation of the question is really, how to deploy and run a Scala app on a machine with only JVM but not Scala or SBT deployed. So it feels like there's no easy way to run it with `java`. Then my question, alternatively, is if it's easy to deploy scala on the target machine? Accordingly to Scala doc, it recommends running scala app via SBT, not even offering a mention on how to properly install scala by itself. So what's the recommendation here? – KFL Aug 02 '18 at 22:27
  • 1
    @ThomasJung I don't get the joke, please explain! – Janac Meena Nov 06 '18 at 21:10
6

Simply running

scala scaaaaaaaaala-1.0.jar 

works for me with 2.11.6

hgrey
  • 3,033
  • 17
  • 21
6

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.

Hartmut Pfarr
  • 5,534
  • 5
  • 36
  • 42
4

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.

Community
  • 1
  • 1
Don Roby
  • 40,677
  • 6
  • 91
  • 113
0

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

Nilesh Shinde
  • 457
  • 5
  • 10