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.
scala foo.bar.Hello
You might also need "-cp .", if the current directory isn't in your CLASSPATH.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With