Skip to content

Instantly share code, notes, and snippets.

@yoyama
Last active December 6, 2020 07:03
Show Gist options
  • Save yoyama/a01c31b601dce9a35bceff75f3c7e9f2 to your computer and use it in GitHub Desktop.
Save yoyama/a01c31b601dce9a35bceff75f3c7e9f2 to your computer and use it in GitHub Desktop.
Scala REPL based app. fail when file name has no extension '.jar' or '.zip'

REPL based application will fail with initialization error of REPL if its jar file name has no extension '.jar' or '.zip'.

import scala.tools.nsc.Settings
import scala.tools.nsc.interpreter.shell.{ILoop, ShellConfig}

object ReplTestMain extends App {
    println("test")
    val settings = new Settings
    settings.usejavacp.value = true
    new TestILoop(ShellConfig(settings)).run(settings)
}

class TestILoop(val shellConfig:ShellConfig) extends ILoop(shellConfig) {
}

In above sample application, we can run REPL as follows.

$ java -jar target/scala-2.13/repl-test-assembly-0.1.0-SNAPSHOT.jar
test
Welcome to Scala 2.13.4 (OpenJDK 64-Bit Server VM, Java 1.8.0_242).
Type in expressions for evaluation. Or try :help.

scala>

But if rename the file to repl-test-assembly-0.1.0-SNAPSHOT, it failed to run as follows.

$ cp target/scala-2.13/repl-test-assembly-0.1.0-SNAPSHOT.jar target/scala-2.13/repl-test-assembly-0.1.0-SNAPSHOT
$ java -jar target/scala-2.13/repl-test-assembly-0.1.0-SNAPSHOT
test
Welcome to Scala 2.13.4 (OpenJDK 64-Bit Server VM, Java 1.8.0_242).
Type in expressions for evaluation. Or try :help.

Failed to initialize compiler: object scala in compiler mirror not found.
** Note that as of 2.8 scala does not assume use of the java classpath.
** For the old behavior pass -usejavacp to scala, or if using a Settings
** object programmatically, settings.usejavacp.value = true.
Exception in thread "main" java.lang.NullPointerException
        at scala.tools.nsc.CompilationUnits$CompilationUnit.<init>(CompilationUnits.scala:44)
        at scala.tools.nsc.CompilationUnits$CompilationUnit.<init>(CompilationUnits.scala:45)
        at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.compile(IMain.scala:735)
        at scala.tools.nsc.interpreter.IMain.bind(IMain.scala:554)
        at scala.tools.nsc.interpreter.IMain.bind(IMain.scala:590)
        at scala.tools.nsc.interpreter.IMain.$anonfun$quietBind$1(IMain.scala:589)
        at scala.tools.nsc.interpreter.shell.ReplReporterImpl.withoutPrintingResults(Reporter.scala:64)
        at scala.tools.nsc.interpreter.IMain.quietBind(IMain.scala:589)

The extension .zip is also OK.

build.sbt

lazy val scalaVer = "2.13.4"
ThisBuild / scalaVersion     := scalaVer
ThisBuild / version          := "0.1.0-SNAPSHOT"
ThisBuild / scalacOptions ++= Seq("-deprecation", "-feature")

lazy val root = (project in file("."))
  .settings(
    name := "repl-test",
    test in assembly := {},
    libraryDependencies ++= Seq(
      "org.scala-lang" % "scala-compiler" % scalaVer,
    ),
  )

project/plugins.sbt

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.15.0")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment