不能 运行 scala 脚本(jdegoes 的 fpmax)
Cannot run a scala script (fpmax by jdegoes)
这是 J. A. De Goes 的精彩网络演讲:https://www.youtube.com/watch?v=sxudIMiOo68 - 强烈推荐给对函数式编程感兴趣的每个人
这是随附的代码要点:
https://gist.github.com/jdegoes/1b43f43e2d1e845201de853815ab3cb9
当我 运行 $ scalac fpmax.scala
时,它会将所有内容编译到一个新目录 fpmax
但是当我 运行 scala App0
时,它给了我错误:
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.access0(URLClassLoader.java:73)
at java.net.URLClassLoader.run(URLClassLoader.java:368)
at java.net.URLClassLoader.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(ScalaClassLoader.scala:45)
at scala.util.control.Exception$Catch.$anonfun$opt(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)
我运行scala 2.12.6.
运行此代码段的正确方法是什么?
我不得不稍微编辑一下 运行 App0。
- 将
def main: Unit
更改为 def main(args: Array[String]): Unit
,以便它具有符合 Scala 规范的带参数的主要方法。
- 让我们编译它:
scala fpmax.scala
。实际上,这将创建 fpmax
目录,其中包含所有 类
- 然后运行 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
请注意,我不会使用 类
进入 fpmax
目录
这是 J. A. De Goes 的精彩网络演讲:https://www.youtube.com/watch?v=sxudIMiOo68 - 强烈推荐给对函数式编程感兴趣的每个人
这是随附的代码要点: https://gist.github.com/jdegoes/1b43f43e2d1e845201de853815ab3cb9
当我 运行 $ scalac fpmax.scala
时,它会将所有内容编译到一个新目录 fpmax
但是当我 运行 scala App0
时,它给了我错误:
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.access0(URLClassLoader.java:73)
at java.net.URLClassLoader.run(URLClassLoader.java:368)
at java.net.URLClassLoader.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(ScalaClassLoader.scala:45)
at scala.util.control.Exception$Catch.$anonfun$opt(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)
我运行scala 2.12.6.
运行此代码段的正确方法是什么?
我不得不稍微编辑一下 运行 App0。
- 将
def main: Unit
更改为def main(args: Array[String]): Unit
,以便它具有符合 Scala 规范的带参数的主要方法。 - 让我们编译它:
scala fpmax.scala
。实际上,这将创建fpmax
目录,其中包含所有 类 - 然后运行 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
请注意,我不会使用 类
进入fpmax
目录