Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala problem - how to run a program that is in a package?

Tags:

scala

I'm embarrassed to ask this, but I cannot figure out how to run a scala program that is defined to be within a package.

Example:

package foo.bar {

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

    }
}

After compiling the Hello.scala file, I get the expected directory structure

-- foo
|   `-- bar
|       |-- Hello$.class
|       `-- Hello.class

Things I've tried:

Nick@Macintosh-2 ~/Desktop/Programming/Scala master$ fsc Hello.scala 
Nick@Macintosh-2 ~/Desktop/Programming/Scala master$ scala Hello
no such file: Hello
Nick@Macintosh-2 ~/Desktop/Programming/Scala master$ scala -classpath foo/bar/ Hello
Exception in thread "main" java.lang.NoClassDefFoundError: Hello (wrong name: foo/bar/Hello)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:676)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:317)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:375)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:242)
    at scala.tools.nsc.ObjectRunner$.findClass(ObjectRunner.scala:29)
    at scala.tools.nsc.ObjectRunner$.classExists(ObjectRunner.scala:42)
    at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:149)
    at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
Nick@Macintosh-2 ~/Desktop/Programming/Scala master$ scala foo/bar/Hello
no such file: foo/bar/Hello
Nick@Macintosh-2 ~/Desktop/Programming/Scala master$ scala foo/bar/Hello.class 

I see plenty of examples online explaining how to package up your files. But I don't see any that show how to run the file from the command line.

Any help would be appreciated.

like image 341
I82Much Avatar asked Jul 07 '10 02:07

I82Much


2 Answers

scala foo.bar.Hello
like image 76
Ken Bloom Avatar answered Oct 03 '22 07:10

Ken Bloom


You might also need "-cp .", if the current directory isn't in your CLASSPATH.

like image 33
Dean Wampler Avatar answered Oct 03 '22 06:10

Dean Wampler