简体   繁体   中英

How to run scala repl with java 11?

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM