开发者

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

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(URLClassLoa开发者_运维百科der.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.


scala foo.bar.Hello


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


Just wanted to add a little clarification for the true newbies here as I ran into a similar problem while trying to start out on Programming in Scala. (I wasn't really interested in following along with all the examples in the interpreter.) When you compile your within-package *.scala file it will place the output *.class file which you need to run in a subdirectory of the same name as the package.

For example, if you are working in the chapter2 package and want to run the Scala file at c:\scalaprojects\src\chapter2\HelloWorld.scala, then from the command line run:

c:\scalaprojects\src\chapter2>scalac HelloWorld.scala

followed by

c:\scalaprojects\src\chapter2>scala chapter2.HelloWorld
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜