Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling Scala 2.12.16 with Gradle and Java 17 getting java.io.IOError: java.lang.RuntimeException: /packages cannot be represented as URI

I have a scala project that is stuck on version 2.12. I am trying to upgrade the Java version to 17, but am getting the infamous java.io.IOError: java.lang.RuntimeException: /packages cannot be represented as URI error. I have looked here and here and here and here (all SO Q&A's) but all to no avail. The common advice between all those answers is either to use Java 8 (not what I want), to upgrade Scala to the latest patch version (already did that) or to upgrade SBT. Regarding the last point, I added the following snippet to my build.gradle.kts file but am still getting the error:

scala {
    zincVersion.set("1.8.0")
}

Any ideas?

like image 494
Jewels Avatar asked Sep 14 '25 18:09

Jewels


1 Answers

Please double check that you're using Scala 2.12.15+, the latest is 2.12.17.

packages cannot be represented as URI was a bug in Scala compiler and Gradle Scala plugin uses Scala compiler (sbt zinc compiler)

https://github.com/scala/bug/issues/11794 Scala 2.13.0 OpenJdk 13: REPL crashes with RuntimeException: /packages cannot be represented as URI

https://github.com/scala/bug/issues/11762 Failed to run scala on Eclipse OpenJ9 VM, Java 13

https://github.com/scala/bug/issues/11608 Compiler crashes when using macros in Scala 2.12 with JDK 13

https://github.com/scala/bug/issues/11381#issuecomment-539727339 GlobalSymbolLoaders#lookupMemberAtTyperPhaseIfPossible throw NullPointerException with JDK12

https://docs.scala-lang.org/overviews/jdk-compatibility/overview.html

Version compatibility table

JDK version Minimum Scala versions
20 3.3.0 (soon), 2.13.11 (soon), 2.12.18 (soon)
19 3.2.0, 2.13.9, 2.12.16
18 3.1.3, 2.13.7, 2.12.15
17 (LTS) 3.0.0, 2.13.6, 2.12.15
11 (LTS) 3.0.0, 2.13.0, 2.12.4, 2.11.12
8 (LTS) 3.0.0, 2.13.0, 2.12.0, 2.11.0

Zinc 1.8.0 uses Scala 2.12.17.

https://github.com/sbt/zinc/blob/v1.8.0/project/Dependencies.scala#L10

val scala210 = "2.10.7"
val scala211 = "2.11.12"
val scala212 = "2.12.17"
val scala213 = "2.13.8"

The issue Update Zinc used by Scala plugin to 1.7.1 https://github.com/gradle/gradle/issues/21392 is still open, the pull request Update Zinc used by Scala plugin to 1.7.1 https://github.com/gradle/gradle/pull/21393 was closed without merging. So Gradle 8.1.0 seems to be still on zinc_2.13:1.6.1

https://github.com/gradle/gradle/blob/v8.1.0/subprojects/scala/build.gradle.kts#L32

compileOnly("org.scala-sbt:zinc_2.13:1.6.1") {
  ...

And zinc 1.6.1 is on Scala 2.13.6

https://github.com/sbt/zinc/blob/v1.6.1/project/Dependencies.scala#L11

val scala210 = "2.10.7"
val scala211 = "2.11.12"
val scala212 = "2.12.15"
val scala213 = "2.13.6"

where packages cannot be represented as URI was supposed to be fixed.

https://docs.gradle.org/current/userguide/scala_plugin.html#sec:configure_zinc_compiler

Zinc compatibility table

Gradle version Supported Zinc versions Zinc coordinates Required Scala version Supported Scala compilation version
7.5 and newer SBT Zinc. Versions 1.6.0 and above. org.scala-sbt:zinc_2.13 Scala 2.13.x is required for running Zinc. Scala 2.10.x through 3.x can be compiled.
...

Scala 2.12.15+, 2.13.6+ are considered to be compatible with Java 17. So if there are still incompatibilities maybe you should open an issue at https://github.com/gradle/gradle/issues, https://github.com/sbt/zinc/issues or https://github.com/scala/bug/


Actually, I can't reproduce packages cannot be represented as URI with Gradle 8.1, Scala 2.12.17, zinc 1.8.0, Java eclipse_adoptium-17-amd64-linux/jdk-17.0.7+7

https://github.com/DmytroMitin/gradledemo

settings.gradle.kts

plugins {
    id("org.gradle.toolchains.foojay-resolver-convention") version "0.4.0"
}

rootProject.name = "gradledemo"
include("app")

app/build.gradle.kts

plugins {
    scala

    application
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.scala-lang:scala-library:2.12.17")

    implementation("com.chuusai:shapeless_2.12:2.3.10")
}

java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(17))
    }
}

scala {
    zincVersion.set("1.8.0")
}

// see (*)
// Without -target or -release: Task :app:compileScala FAILED: jvm-1.17 is not a valid choice for -target, bad option: -target:jvm-1.17
// With -target: [Warn] : -target is deprecated: Scala 2.12 cannot emit valid class files for targets newer than 8 (this is possible with Scala 2.13). Use -release to compile against a specific platform API version.
tasks.withType<ScalaCompile>() {
    targetCompatibility = ""
    scalaCompileOptions.additionalParameters = listOf("-release:17")
}

application {
    mainClass.set("gradledemo.App")
}

app/src/main/scala/gradledemo/App.scala

package gradledemo

import shapeless._

object App {
  def main(args: Array[String]): Unit = {
    println(1 :: "a" :: true :: HNil)
  }
}
$ gradle clean

BUILD SUCCESSFUL in 533ms
1 actionable task: 1 executed

$ gradle run

> Task :app:run
1 :: a :: true :: HNil

BUILD SUCCESSFUL in 4s
2 actionable tasks: 2 executed

How to reproduce packages cannot be represented as URI? Please prepare reproduction.

(*) Scala build error on Java 11 using intellijidea

like image 131
Dmytro Mitin Avatar answered Sep 17 '25 09:09

Dmytro Mitin