How to run/test my code

Hello. I am at the beginning of my learning path in Scala (not new to programming though) and I am having troubles running and testing my code. I am following the Scala track in Exercism (Scala on Exercism) on my Mac with IntelliJ.

The exercises there come with a test (that is executed using sbt test in the right directory) but some of the exercises have tests that don’t work, throwing the error /packages cannot be represented as URI. I have googled it and after a few StackOverflow answers, I have not been able to run it without errors. The info about my Scala, Java and sbt installation is the following (all three have been installed using brew):

[info] [launcher] getting org.scala-sbt sbt 1.5.6  (this may take some time)...
WARNING: A terminally deprecated method in java.lang.System has been called
WARNING: System::setSecurityManager has been called by sbt.TrapExit$ (file:/Users/javier.lopez/.sbt/boot/scala-2.12.14/org.scala-sbt/sbt/1.5.6/run_2.12-1.5.6.jar)
WARNING: Please consider reporting this to the maintainers of sbt.TrapExit$
WARNING: System::setSecurityManager will be removed in a future release
sbt version in this project: 1.5.6
sbt script version: 1.5.6

Scala code runner version 2.13.7 -- Copyright 2002-2021, LAMP/EPFL and Lightbend, Inc.

openjdk version "17.0.1" 2021-10-19
OpenJDK Runtime Environment (build 17.0.1+12-39)
OpenJDK 64-Bit Server VM (build 17.0.1+12-39, mixed mode, sharing)

“I will compile and run my code from IntelliJ”, I have thought, after giving up on the tests. But I am unable to do so either, because I have no run button anywhere. In my build configuration I have set either default JSDK or java 17.

I have tried also a couple of Run/Debug configurations, but I don’t think none of them is correct:

I have reinstalled as well the IDE. So, at this point, I thought, “OK, I will just execute the code myself from the terminal”. The code is the following (pretty simple):

object Bob extends App {
      def response(statement: String): String = {
        if (statement.takeRight(1) == "?") {
          println("whoa")
          "Whoa, chill out!"
        }
        else {
          println("whatever")
          "Whatever."
        }
      }
    }

I have compiled it from the terminal with:
scalac Bob.scala
And then I have tried to run with several commands:

scala response
scala Bob.response
scala Bob.Bob.response
scala Bob.response “test sentence”

Getting either an error or nothing.

I am lost and I have no idea how to solve the situation. Any help would be appreciated.

Thank you very much

Not sure about IntelliJ (since I don’t use it).
But the problem when you try to run the code in the console is that you can’t call arbitrary methods of a class, you can only run a main

Try this instead:

// File: Bob.scala
object Bob {
  def response(statement: String): String =
    if (statement.takeRight(1) == "?") {
      println("whoa")
      "Whoa, chill out!"
    } else {
      println("whatever")
      "Whatever."
    }
  
  def main(args: Array[String]): Unit = {
    args.toList.foreach { arg =>
      val result = response(statement = arg)
      println(s"Response for ${arg}: ${result}")
    }
  }
}

Which you can run like:

scalac Bob.scala
scala Bob
1 Like

The error “/packages cannot be represented as URI” suggests https://github.com/sbt/sbt/issues/5093

If so, you need to either use a newer sbt or an older Java. (Preferably a newer sbt! Trying older Java would be a last resort, as the Scala+sbt toolchain definitely works on JDK 17.)

The transcript in your post appears to show that you are using sbt 1.5.6, which should in fact be new enough to work, regardless of your JDK version.

Can you confirm that you’re getting “/packages cannot be represented as URI” even with sbt 1.5.6? Perhaps that error can have a different cause.

1 Like

Thanks for answering. Yes, I can confirm.

I paste here part of the log:

WARNING: A terminally deprecated method in java.lang.System has been called
WARNING: System::setSecurityManager has been called by sbt.TrapExit$ (file:/Users/javier.lopez/.sbt/boot/scala-2.12.14/org.scala-sbt/sbt/1.5.6/run_2.12-1.5.6.jar)
WARNING: Please consider reporting this to the maintainers of sbt.TrapExit$
WARNING: System::setSecurityManager will be removed in a future release
[info] welcome to sbt 1.5.6 (Homebrew Java 17.0.1)
[info] loading global plugins from /Users/javier.lopez/.sbt/1.0/plugins
[info] loading project definition from /Users/javier.lopez/Exercism/scala/bob/project
[info] loading settings for project bob from build.sbt ...
[info] set current project to bob (in build file:/Users/javier.lopez/Exercism/scala/bob/)
[info] compiling 1 Scala source to /Users/javier.lopez/Exercism/scala/bob/target/scala-2.12/test-classes ...
[error] ## Exception when compiling 1 sources to /Users/javier.lopez/Exercism/scala/bob/target/scala-2.12/test-classes
[error] java.io.IOError: java.lang.RuntimeException: /packages cannot be represented as URI
[error] java.base/jdk.internal.jrtfs.JrtPath.toUri(JrtPath.java:175)
[error] scala.tools.nsc.classpath.JrtClassPath.asURLs(DirectoryClassPath.scala:213)

Thanks! I’ll give it a try

I’m mystified, then. I think you could report this to https://github.com/sbt/sbt/issues

1 Like

There was a solution reported here https://github.com/sbt/sbt/issues/5509#issuecomment-619508472

I think you should give this a try as well.

I have seen this issue before. IIRC I uninstalled my JDKs then installed from https://adoptopenjdk.net/ or just not using multiple tools to manage my JDKs.

1 Like

Ah, nice catch. 5093 said 5509 was a duplicate, so I didn’t even look at 5509.

I’ve now left a breadcrumb on 5093 to help future seekers find that comment on 5509.

The proposal in Github has not worked, although I have right now sbt 1.5.7.

I have uninstalled my JDKs. It turns out that I had adotopenjdk8 in the JDK folder, while JAVA 17.

I have tried to uninstall Java as well (with all the rm commands that appear in Google for that) and even after restarting my mac I still have java --version command working. So I don’t want to install again the JDKs from adoptopen until I get Java uninstalled. Or the JDK is independent of Java?

do you have an IDE installed? Installing from multiple sources will put things in places you’re not aware of.