41

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.

0

5 Answers 5

51

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!
Sign up to request clarification or add additional context in comments.

3 Comments

I would prefer a personal Descartes or Newton. :-)
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?
@ThomasJung I don't get the joke, please explain!
7

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.

Comments

6

Simply running

scala scaaaaaaaaala-1.0.jar 

works for me with 2.11.6

1 Comment

that's not the question. of course you can run it with scala. the question is how to run it w/o scala.
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.

Comments

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

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.