1

How can I run a *.scala script in interpreting mode? For example I have simple.scala:

// simple.scala
println("a simple program")

If use scala simple.scala, I got

error: Compile server encountered fatal condition: javax/tools/DiagnosticListener
java.lang.ClassNotFoundException: javax.tools.DiagnosticListener
        at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:433)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:586)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
        at scala.tools.util.PathResolverBase$Calculated$.javaBootClasspath(PathResolver.scala:277)
        at scala.tools.util.PathResolverBase$Calculated$.basis(PathResolver.scala:283)
        at scala.tools.util.PathResolverBase$Calculated$.containers$lzycompute(PathResolver.scala:293)
        at scala.tools.util.PathResolverBase$Calculated$.containers(PathResolver.scala:293)
        at scala.tools.util.PathResolverBase.containers(PathResolver.scala:309)
        at scala.tools.util.PathResolver.computeResult(PathResolver.scala:341)
        at scala.tools.util.PathResolver.computeResult(PathResolver.scala:332)
        at scala.tools.util.PathResolverBase.result(PathResolver.scala:314)
        at scala.tools.nsc.backend.JavaPlatform$class.classPath(JavaPlatform.scala:28)
        at scala.tools.nsc.Global$GlobalPlatform.classPath(Global.scala:115)
        at scala.tools.nsc.Global.scala$tools$nsc$Global$$recursiveClassPath(Global.scala:131)
        at scala.tools.nsc.Global$GlobalMirror.rootLoader(Global.scala:64)
        at scala.reflect.internal.Mirrors$Roots$RootClass.<init>(Mirrors.scala:307)
        at scala.reflect.internal.Mirrors$Roots.RootClass$lzycompute(Mirrors.scala:321)
        at scala.reflect.internal.Mirrors$Roots.RootClass(Mirrors.scala:321)
        at scala.reflect.internal.Mirrors$Roots$EmptyPackageClass.<init>(Mirrors.scala:330)
        at scala.reflect.internal.Mirrors$Roots.EmptyPackageClass$lzycompute(Mirrors.scala:336)
        at scala.reflect.internal.Mirrors$Roots.EmptyPackageClass(Mirrors.scala:336)
        at scala.reflect.internal.Mirrors$Roots.EmptyPackageClass(Mirrors.scala:276)
        at scala.reflect.internal.Mirrors$RootsBase.init(Mirrors.scala:250)
        at scala.tools.nsc.Global.rootMirror$lzycompute(Global.scala:73)
        at scala.tools.nsc.Global.rootMirror(Global.scala:71)
        at scala.tools.nsc.Global.rootMirror(Global.scala:39)
        at scala.reflect.internal.Definitions$DefinitionsClass.ObjectClass$lzycompute(Definitions.scala:257)
        at scala.reflect.internal.Definitions$DefinitionsClass.ObjectClass(Definitions.scala:257)
        at scala.reflect.internal.Definitions$DefinitionsClass.init(Definitions.scala:1390)
        at scala.tools.nsc.Global$Run.<init>(Global.scala:1242)
        at scala.tools.nsc.StandardCompileServer.session(CompileServer.scala:151)
        at scala.tools.util.SocketServer$$anonfun$doSession$1$$anonfun$apply$1.apply(SocketServer.scala:74)
        at scala.tools.util.SocketServer$$anonfun$doSession$1$$anonfun$apply$1.apply(SocketServer.scala:74)
        at scala.util.DynamicVariable.withValue(DynamicVariable.scala:58)
        at scala.Console$.withOut(Console.scala:65)
        at scala.tools.util.SocketServer$$anonfun$doSession$1.apply(SocketServer.scala:74)
        at scala.tools.util.SocketServer$$anonfun$doSession$1.apply(SocketServer.scala:69)
        at scala.tools.nsc.io.Socket.applyReaderAndWriter(Socket.scala:49)
        at scala.tools.util.SocketServer.doSession(SocketServer.scala:69)
        at scala.tools.util.SocketServer.loop$1(SocketServer.scala:85)
        at scala.tools.util.SocketServer.run(SocketServer.scala:97)
        at scala.tools.nsc.CompileServer$$anonfun$execute$2$$anonfun$apply$mcZ$sp$1.apply$mcZ$sp(CompileServer.scala:218)
        at scala.tools.nsc.CompileServer$$anonfun$execute$2$$anonfun$apply$mcZ$sp$1.apply(CompileServer.scala:213)
        at scala.tools.nsc.CompileServer$$anonfun$execute$2$$anonfun$apply$mcZ$sp$1.apply(CompileServer.scala:213)
        at scala.util.DynamicVariable.withValue(DynamicVariable.scala:58)
        at scala.Console$.withOut(Console.scala:53)
        at scala.tools.nsc.CompileServer$$anonfun$execute$2.apply$mcZ$sp(CompileServer.scala:213)
        at scala.tools.nsc.CompileServer$$anonfun$execute$2.apply(CompileServer.scala:213)
        at scala.tools.nsc.CompileServer$$anonfun$execute$2.apply(CompileServer.scala:213)
        at scala.util.DynamicVariable.withValue(DynamicVariable.scala:58)
        at scala.Console$.withErr(Console.scala:80)

But I can open the interpreter by command scala and then > println("a simple program") It works correctly. Also, if I compile a more complete .scala (with an object which contains a main function) by scalac then run it by scala, things goes well.

I wonder if I can run a .scala script in the interpreter. Is there any thing wrong?


Update: After I update scala from 2.11 to 3.20, there is no javax.tools.DiagnosticListener exceptions. But when I run scala -explain simple.scala, the error is:

-- [E103] Syntax Error: ~/simple.scala:2:0
2 |println("a simple program")
  |^^^^^^^
  |Illegal start of toplevel definition
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | A statement is an import or export, a definition or an expression.
  | Some statements are only allowed in certain contexts
   -----------------------------------------------------------------------------
1 error found
Error: Errors encountered during compilation

JAVA version: openjdk 16.0.1 2021-04-20

4
  • If you want to write and run simple scripts give it a look to scala-cli. If you would rather write bigger programs then use sbt and use either the native packager or assembly plugin to produce the final executables. Commented Oct 13, 2022 at 12:12
  • 2
    Try scala -nocompdaemon. But also, what version of Scala are you using? I suspect you're on an ancient version and that might be part of the difficulty. Commented Oct 14, 2022 at 11:59
  • @SethTisue Thank you. I installed scala directly by apt-get, its version is 2.11. Now I re-installed 3.2. There are no javax.tools.DiagnosticListener exception. However, other error occurs. Commented Oct 17, 2022 at 2:47
  • Unlike Scala 2, Scala 3's scala command doesn't include a script runner, as per github.com/lampepfl/dotty-feature-requests/issues/38. You either have to define a @main method, or (my personal recommendation) use scala-cli instead (scala-cli.virtuslab.org/scripting). Commented Oct 19, 2022 at 6:58

1 Answer 1

1

As mentioned by Seth Tisue, the following works:

def main(args: Array[String]): Unit =
    // simple.scala
    println("a simple program")

if you invoke it like this:

scala3 ~/simple.scala

Although by default, there is no script called scala3, if scala3 is installed here: /opt/scala3-3.2.2, then the command line would be:

/opt/scala3-3.2.2/bin/scala ~/simple.scala

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.