tags:

views:

264

answers:

2

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?

+3  A: 

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
Synesso
I can't run the first one as I have no Scala installed (sbt downloaded it and using internally). The second one does not work.
Łukasz Lew
java -cp project/boot/scala-2.7.7/lib/scala-library.jar:. -jar target/scala_2.7.7/test_2.7.7-1.0.jar Hi # Does not work.
Łukasz Lew
You can't mix -cp and -jar with the java executable. Include your test_2.7.7-1.0.jar in the classpath. If you're on windows, the separator is ; not :So, please try:java -cp project/boot/scala-2.7.7/lib/scala-library.jar:target/scala_2.7.7/test_2.7.7-1.0.jar Hi
Synesso
+1  A: 

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.

Arjan Blokzijl
I created using scratch project - s in (y/N/s). Using new sbt. It downloaded scala itself.
Łukasz Lew