question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

scala3.library.3: Invalid module name: '3' is not a Java identifier

See original GitHub issue

Compiler version

3.0.0

Minimized code

in pom.xml add to dependencies:

<dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala3-library_3</artifactId>
            <version>3.0.0</version>
</dependency>

and then try to build the app with mvn. (please read on for detailed explanation).

Output

[WARNING] There are 1 pathException(s). The related dependencies will be ignored.
[WARNING] Dependency: /home/makingthematrix/.m2/repository/org/scala-lang/scala3-library_3/3.0.0/scala3-library_3-3.0.0.jar
   - exception: Unable to derive module descriptor for /home/makingthematrix/.m2/repository/org/scala-lang/scala3-library_3/3.0.0/scala3-library_3-3.0.0.jar
   - cause: scala3.library.3: Invalid module name: '3' is not a Java identifier

Expectation

There should be no warning about “‘3’ is not a Java identifier” and no later errors when the code tries to access the Scala library.

Details

This will happen in projects which use together Scala, Maven, and Java 9+. It’s not a common combination, but currently this is the way to write programs like this one - an Android app written in Scala 3 that uses JavaFX as GUI, and is built with GraalVM native image. Currently there is no way to do it with SBT. But Maven works pretty well. Except this one issue.

Since Java 9, the Scala way of adding a suffix with the Scala version at the end of artifact id of the library is invalid - hence the error above. An easy way to fix it is to add a line to MANIFEST.MF of the library’s jar file:

Automatic-Module-Name: <library name> 

This can be done from SBT. For example in case of scala3-library_3 it’s enough to add something like this to dotty/project/Build.scala:

lazy val `scala3-library` = project.in(file("library")).
    asDottyLibrary(NonBootstrapped).
    settings(
      Compile / packageBin / packageOptions += Package.ManifestAttributes("Automatic-Module-Name" -> "scala3-library")
    )
  lazy val `scala3-library-bootstrapped`: Project = project.in(file("library")).
    asDottyLibrary(Bootstrapped).
    settings(
      Compile / packageBin / packageOptions += Package.ManifestAttributes("Automatic-Module-Name" -> "scala3-library")
    )

Please note that usually it’s better to hardcode the value scala3-library instead of trying to derive it from the library name.

Actually, the same problem exists for all Scala 2 libraries. I wrote a Maven plugin that fixes this downstream - i.e. it unzips the downloaded jar file of the given Scala library, modifies MANIFEST.MF, and zips it again - but it would be better to have it fixed at the source. Here’s the link to the plugin page where you can read more about this issue.

I’m planning to create a PR here in the Dotty project and when it’s done I will link it to this bug report. This is the first time I do it, so sorry if I mess up something.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
smartercommented, Jun 22, 2021

I’ve opend https://github.com/sbt/sbt/issues/6561 to discuss this further.

0reactions
bishaboshacommented, Aug 2, 2021

fixed by #12903

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add Automatic-Module-Names to MANIFEST.MF files ... - GitHub
The values are derived from dottyOrganization + the project name. ... scala3.library.3: Invalid module name: '3' is not a Java identifier #12899.
Read more >
Java identifier rules problem in library module names
IllegalArgumentException: mp3spi1.9.4: Invalid module name: '9' is not a Java identifier. I am running Java 10 jdk-10.0.2 in the most recent version of ......
Read more >
is there a way i can fix this invalid module name error?
1 Answer 1 · Rename the jar file to something like just jlfgr.jar · In the module-info.java file, add the following sentence: requires...
Read more >
Scala 3 - The Scala Programming Language
scala.swing - A convenient wrapper around Java's GUI framework called Swing (scala-swing.jar). Automatic imports. Identifiers in the scala package and the scala ...
Read more >
Invalid module name: '' is not a Java identifier : r/eclipse - Reddit
I'm trying to make a minecraft mod for 1.18 in Eclipse, I just set everything up and I wanted to make sure it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found