scalacompilationscalac

Cannot run a scala script (fpmax by jdegoes)


Here's a brilliant web talk by J. A. De Goes: https://www.youtube.com/watch?v=sxudIMiOo68 - highly recommended for everyone interested in functional programming

And here's accompanying code gist: https://gist.github.com/jdegoes/1b43f43e2d1e845201de853815ab3cb9

When I run $ scalac fpmax.scala, it compiles everything to a new directory fpmax

But then when I run scala App0, it gives me error:

Exception in thread "main" java.lang.NoClassDefFoundError: App0 (wrong name: fpmax/App0)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:348)
        at scala.reflect.internal.util.ScalaClassLoader.$anonfun$tryClass$1(ScalaClassLoader.scala:45)
        at scala.util.control.Exception$Catch.$anonfun$opt$1(Exception.scala:242)
        at scala.util.control.Exception$Catch.apply(Exception.scala:224)
        at scala.util.control.Exception$Catch.opt(Exception.scala:242)
        at scala.reflect.internal.util.ScalaClassLoader.tryClass(ScalaClassLoader.scala:45)
        at scala.reflect.internal.util.ScalaClassLoader.tryToLoadClass(ScalaClassLoader.scala:39)
        at scala.reflect.internal.util.ScalaClassLoader.tryToLoadClass$(ScalaClassLoader.scala:39)
        at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.tryToLoadClass(ScalaClassLoader.scala:125)
        at scala.reflect.internal.util.ScalaClassLoader$.classExists(ScalaClassLoader.scala:150)
        at scala.tools.nsc.GenericRunnerCommand.guessHowToRun(GenericRunnerCommand.scala:36)
        at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:55)
        at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:18)
        at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:42)
        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:101)
        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

I run scala 2.12.6.

What would be correct way to run this code snippet?


Solution

  • I had to edit it a bit to run App0.

    1. Change def main: Unit to def main(args: Array[String]): Unit, so that it has main method with args as per Scala spec.
    2. Let's compile it: scala fpmax.scala. Indeed, this will create fpmax directory with all classes in it
    3. Then run the App0:
    scala fpmax.App0                                                             5.50   ✔   12:21    19.03.19 
    What is your name?
    Alex
    Hello, Alex, welcome to the game!
    Dear Alex, please guess a number from 1 to 5:
    4
    You guessed wrong, Alex! The number was: 3
    Do you want to continue, Alex?
    n
    

    Note, I am NOT going inside the fpmax directory with classes