如何在 Java 11 中运行 Scala REPL?

huangapple go评论83阅读模式
英文:

How to run scala repl with java 11?

问题

我已经在我的Ubuntu 20.04上安装了Java 11;

Java版本:

$ java -version
openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)

Scala版本:

$ scala -version
Scala代码运行器版本 2.11.12 -- 版权所有 2002-2017,LAMP/EPFL

当我尝试运行Scala REPL时,我遇到了以下错误:

欢迎使用Scala 2.11.12 (OpenJDK 64-Bit Server VM,Java 11.0.8)键入表达式进行评估。或尝试 :help。
主线程中的异常 "main" java.lang.NoClassDefFoundError: javax/script/Compilable
	at scala.tools.nsc.interpreter.ILoop.createInterpreter(ILoop.scala:118)
	at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1$$anonfun$startup$1$1$$anonfun$apply$1.apply(ILoop.scala:971)
	...

如果我尝试使用Java 8运行(使用update-alternatives --config java更改版本),它可以正常工作:

REPL:

$ scala
cat: /usr/lib/jvm/java-8-openjdk-amd64/jre/release: 文件或目录不存在
欢迎使用Scala 2.11.12 (OpenJDK 64-Bit Server VM,Java 1.8.0_265)键入表达式进行评估。或尝试 :help。

如何让Scala REPL在Java 11中正常工作?我还每次在切换Java 8和11之间时更改了我的.zshrc文件中的JAVA_HOME

英文:

I've installed java 11 on my ubuntu 20.04;

The java version:

$ java -version
penjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)

The scala version:

$ scala -version
Scala code runner version 2.11.12 -- Copyright 2002-2017, LAMP/EPFL

When i try to run the scala repl i got the error:

Welcome to Scala 2.11.12 (OpenJDK 64-Bit Server VM, Java 11.0.8).
Type in expressions for evaluation. Or try :help.
Exception in thread "main" java.lang.NoClassDefFoundError: javax/script/Compilable
	at scala.tools.nsc.interpreter.ILoop.createInterpreter(ILoop.scala:118)
	at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1$$anonfun$startup$1$1$$anonfun$apply$1.apply(ILoop.scala:971)
	at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1$$anonfun$startup$1$1$$anonfun$apply$1.apply(ILoop.scala:971)

scala> 	at scala.tools.nsc.interpreter.ILoop.savingReader(ILoop.scala:96)
	at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1$$anonfun$startup$1$1.apply(ILoop.scala:970)
	at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:990)
	at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:891)
	at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:891)
	at scala.reflect.internal.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:97)
	at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:891)
	at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:74)
	at scala.tools.nsc.MainGenericRunner.run$1(MainGenericRunner.scala:87)
	at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:98)
	at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:103)
	at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
[ERROR] Failed to disable litteral next character
java.lang.InterruptedException
	at java.base/java.lang.Object.wait(Native Method)
	at java.base/java.lang.Object.wait(Object.java:328)
	at java.base/java.lang.ProcessImpl.waitFor(ProcessImpl.java:495)
	at jline.internal.TerminalLineSettings.waitAndCapture(TerminalLineSettings.java:339)
	at jline.internal.TerminalLineSettings.exec(TerminalLineSettings.java:311)
	at jline.internal.TerminalLineSettings.stty(TerminalLineSettings.java:282)
	at jline.internal.TerminalLineSettings.undef(TerminalLineSettings.java:158)
	at jline.UnixTerminal.disableLitteralNextCharacter(UnixTerminal.java:194)
	at jline.console.ConsoleReader.readLine(ConsoleReader.java:2450)
	at jline.console.ConsoleReader.readLine(ConsoleReader.java:2373)
	at jline.console.ConsoleReader.readLine(ConsoleReader.java:2361)
	at scala.tools.nsc.interpreter.jline.InteractiveReader.readOneLine(JLineReader.scala:59)
	at scala.tools.nsc.interpreter.InteractiveReader$class.readLine(InteractiveReader.scala:38)
	at scala.tools.nsc.interpreter.jline.InteractiveReader.readLine(JLineReader.scala:27)
	at scala.tools.nsc.interpreter.SplashReader.readLine(InteractiveReader.scala:142)
	at scala.tools.nsc.interpreter.SplashLoop.run(InteractiveReader.scala:71)
	at java.base/java.lang.Thread.run(Thread.java:834)

If i try to run using java8 (changing the version using update-alternatives --config java) it works:

Repl:

$ scala
cat: /usr/lib/jvm/java-8-openjdk-amd64/jre/release: Arquivo ou diretório não encontrado
Welcome to Scala 2.11.12 (OpenJDK 64-Bit Server VM, Java 1.8.0_265).
Type in expressions for evaluation. Or try :help.

scala> 

How can i get scala repl to work along java 11? i've also changed the JAVA_HOME on my .zshrc file everytime i change between java8 and 11.

答案1

得分: 4

Scala 2.11.12 应当与 JDK 11 兼容,参见 版本兼容性表

## 版本兼容性表

| JDK 版本   | 最低 Scala 版本                 | 推荐 Scala 版本                                           |
|:----------:|:-------------------------------|:---------------------------------------------------------|
| 13, 14     | 2.13.2, 2.12.11                | 2.13.3, 2.12.12                                          |
| 12         | 2.13.1, 2.12.9                 | 2.13.3, 2.12.12                                          |
| 11         | 2.13.0, 2.12.4, 2.11.12        | 2.13.3, 2.12.12, 2.11.12                                 |
| 8          | 2.13.0, 2.12.0, 2.11.0, 2.10.2 | 2.13.3, 2.12.12, 2.11.12, 2.10.7                         |
| 6, 7       | 2.11.0, 2.10.0                 | 2.11.12, 2.10.7                                          |

在我的机器上执行

scala --scala-version 2.11.12 -C--jvm=11

使用 scala-runners 没有报错

➜  ~ scala --scala-version 2.11.12 -C--jvm=11
欢迎使用 Scala 2.11.12 (OpenJDK 64-Bit Server VM, Java 11.0.7)。
输入表达式以进行评估。或者尝试使用 :help。

scala> sys.props("java.specification.version")
res0: String = 11

scala> util.Properties.versionString
res1: String = version 2.11.12

scala>

注意 -C--jvm=11 参数指定了由 coursier 下载的 JDK 版本。

英文:

Scala 2.11.12 should be compatible with JDK 11 according to Version compatibility table

## Version compatibility table

| JDK version | Minimum Scala versions           | Recommended Scala versions                                 |
|:-----------:|:---------------------------------|:-----------------------------------------------------------|
| 13, 14      | 2.13.2, 2.12.11                  | 2.13.3, 2.12.12                                            |
| 12          | 2.13.1, 2.12.9                   | 2.13.3, 2.12.12                                            |
| 11          | 2.13.0, 2.12.4, 2.11.12          | 2.13.3, 2.12.12, 2.11.12                                   |
| 8           | 2.13.0, 2.12.0, 2.11.0, 2.10.2   | 2.13.3, 2.12.12, 2.11.12, 2.10.7                           |
| 6, 7        | 2.11.0, 2.10.0                   | 2.11.12, 2.10.7                                            |

On my machine executing

scala --scala-version 2.11.12 -C--jvm=11

with scala-runners gives no error

➜  ~ scala --scala-version 2.11.12 -C--jvm=11
Welcome to Scala 2.11.12 (OpenJDK 64-Bit Server VM, Java 11.0.7).
Type in expressions for evaluation. Or try :help.

scala> sys.props("java.specification.version")
res0: String = 11

scala> util.Properties.versionString
res1: String = version 2.11.12

scala>

Note the -C--jvm=11 argument specifies JDK version which is downloaded by coursier.

huangapple
  • 本文由 发表于 2020年10月6日 23:43:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64229275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定