Reputation: 50338
I run:
$ echo 'object Hi { def main(args: Array[String]) { println("Hi!") } }' > hw.scala
$ sbt
> warn
Set log level to warn
> run
Hi!
> package
$ java -jar target/scala_2.7.7/test_2.7.7-1.0.jar
Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
Why can't I run this jar package this way?
Upvotes: 5
Views: 2884
Reputation: 6888
I don't know which version of sbt you're using, or what project setup you have used, but normally your hw.scala file should be placed in src/main/scala directory for sbt to find it. Also, as synesso remarked, the scala runtime seems to be absent. Normally sbt will just download these when creating a new project. I just tried it using sbt 0.7.3 in a fres project, and this works:
$ sbt Project does not exist, create new project? (y/N/s) y Name: test Organization: test Version [1.0]: Scala version [2.7.7]: sbt version [0.7.3]: Getting Scala 2.7.7 ... :: retrieving :: org.scala-tools.sbt#boot-scala confs: [default] 2 artifacts copied, 0 already retrieved (9911kB/26ms) Getting org.scala-tools.sbt sbt_2.7.7 0.7.3 ... :: retrieving :: org.scala-tools.sbt#boot-app confs: [default] 15 artifacts copied, 0 already retrieved (4023kB/25ms) [success] Successfully initialized directory structure. [info] Building project test 1.0 against Scala 2.7.7 [info] using sbt.DefaultProject with sbt 0.7.3 and Scala 2.7.7 > exit $echo 'object Hi { def main(args: Array[String]) { println("Hi!") } }' > src/main/scala/hw.scala > sbt [info] Building project test 1.0 against Scala 2.7.7 [info] using sbt.DefaultProject with sbt 0.7.3 and Scala 2.7.7 > run Hi! > package [info] [info] == compile == [info] Source analysis: 0 new/modified, 0 indirectly invalidated, 0 removed. [info] Compiling main sources... [info] Nothing to compile. [info] Post-analysis: 2 classes. [info] == compile == [info] [info] == package == [info] == package == [success] Successful.
Upvotes: 0
Reputation: 39018
You are attempting to run it without the scala runtime. Either use the scala executable:
scala -jar target/scala_2.7.7/test_2.7.7-1.0.jar
or, add the scala jar to the classpath
java -cp target/scala_2.7.7/test_2.7.7-1.0.jar:$PATH_TO_SCALA_JAR Hi
Upvotes: 4