1

Code:

object HelloWorld{
    def main(args: Array[String]){
        println("Hello, world!")
    }
}

Environment Variables:

On windows:

JAVA_HOME = C:\Program Files\Java\jdk1.7.0_07
CLASSPATH = .;C:\Program Files\Java\jdk1.7.0_07\lib;C:\Software\scala-2.9.2\lib
PATH = %PATH%;C:\Program Files\Java\jdk1.7.0_07\bin;C:\Software\scala-2.9.2\bin
SCALA_HOME = C:\Software\scala-2.9.2

Steps:

scala HelloWorld.scala  //Correctly output Hello,world!
scalac HelloWorld.scala //No Compilation error
java HelloWorld  //Errors shown below

Error:

Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at HelloWorld.main(HelloWorld.scala)
Caused by: java.lang.ClassNotFoundException: scala.ScalaObject
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 13 more

Sorry for the noobie question, but I have tried for a long time and could not get it work.

Tried scala -save to jar file and run java -jar; also tried change classpath like adding scala-library.jar, and still not working.

texasbruce
  • 13,867
  • 12
  • 57
  • 117

2 Answers2

0

This works for me (Linux):

java -cp scala-library-2.9.1.jar:. HelloWorld

scala-library-2.9.1.jar can be found here.

On Windows you have to use semicolon as CLASSPATH separator:

java -cp scala-library-2.9.1.jar;. HelloWorld

Basically you need to add both scala-library-*.jar JAR and .class files with your code. In the examples above is simply use . to denote .class files in current directory.

Tomasz Nurkiewicz
  • 311,858
  • 65
  • 665
  • 652
0

It is a great pain if there are hundreds of them :(

It is not, you just put them jars in your #JRE#/lib/ext directory

user461519
  • 219
  • 2
  • 6