Scala Hello World:NoSuchMethodError:java.nio.ByteBuffer.clear

时间:2018-08-29 21:53:05

标签: scala

当我尝试在Scala中编译一个简单的hello world程序时:

Hello.scala

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

scalac Hello.scala。我得到

error: java.lang.NoSuchMethodError: java.nio.ByteBuffer.clear()Ljava/nio/ByteBuffer;
        at scala.tools.nsc.io.SourceReader.read(SourceReader.scala:61)
        at scala.tools.nsc.io.SourceReader.read(SourceReader.scala:40)
        at scala.tools.nsc.io.SourceReader.read(SourceReader.scala:49)
        at scala.tools.nsc.Global.getSourceFile(Global.scala:395)
        at scala.tools.nsc.Global.getSourceFile(Global.scala:401)
        at scala.tools.nsc.Global$Run$$anonfun$30.apply(Global.scala:1607)
        at scala.tools.nsc.Global$Run$$anonfun$30.apply(Global.scala:1607)
        at scala.collection.immutable.List.map(List.scala:284)
        at scala.tools.nsc.Global$Run.compile(Global.scala:1607)
        at scala.tools.nsc.Driver.doCompile(Driver.scala:32)
        at scala.tools.nsc.MainClass.doCompile(Main.scala:23)
        at scala.tools.nsc.Driver.process(Driver.scala:51)
        at scala.tools.nsc.Driver.main(Driver.scala:64)
        at scala.tools.nsc.Main.main(Main.scala)
Exception in thread "main" java.lang.NoSuchMethodError: java.nio.ByteBuffer.clear()Ljava/nio/ByteBuffer;
        at scala.tools.nsc.io.SourceReader.read(SourceReader.scala:61)
        at scala.tools.nsc.io.SourceReader.read(SourceReader.scala:40)
        at scala.tools.nsc.io.SourceReader.read(SourceReader.scala:49)
        at scala.tools.nsc.Global.getSourceFile(Global.scala:395)
        at scala.tools.nsc.Global.getSourceFile(Global.scala:401)
        at scala.tools.nsc.Global$Run$$anonfun$30.apply(Global.scala:1607)
        at scala.tools.nsc.Global$Run$$anonfun$30.apply(Global.scala:1607)
        at scala.collection.immutable.List.map(List.scala:284)
        at scala.tools.nsc.Global$Run.compile(Global.scala:1607)
        at scala.tools.nsc.Driver.doCompile(Driver.scala:32)
        at scala.tools.nsc.MainClass.doCompile(Main.scala:23)
        at scala.tools.nsc.Driver.process(Driver.scala:51)
        at scala.tools.nsc.Driver.main(Driver.scala:64)
        at scala.tools.nsc.Main.main(Main.scala)
在具有scalac版本2.11.12和Java版本1.8.0_181的Ubuntu 18上

。奇怪的是,同一程序将在scalac版本2.12.6和Java 1.8.0_111的macOS上编译并运行。

我的两个问题是:为什么会这样?我该如何解决?

谢谢。

1 个答案:

答案 0 :(得分:1)

在scala 2.11.12中,我必须安装openjdk版本“ 10.0.2”。这完全是通过命令openjdk-11-jdk-headless来完成的。

https://docs.scala-lang.org/overviews/jdk-compatibility/overview.html

运行openjdk版本8时遇到相同的错误。