Hardware/Software setup:
IDE: Eclipse IDE Version 2024-12 (4.34.0)
JDK: jdk-21
OS: Windows 11 Pro 24H2
CPU: 12th Gen Intel(R) Core(TM) i5-12600KF
Background/Project Configuration
I created a modular java project in eclipse.
I am not using Maven/Gradle as part of the project.
I manually downloaded gson-2.13.1.jar downloaded from the maven repository.
I created a new folder "\lib" in my project directory, and put the jar file there.
I used project->properties->Libraries Tab->Module Path->Add External JARs... to add the jar file to my module path.
In the "Module Dependencies" tab I see that "com.google.gson" shows up.
In my source files I can do "import com.google.gson.*;" without problems. Eclipse sees the classes and the code completion menus work as expected.
But when I try to put "requires com.google.gson;" in module-info.java i see error.
"com.google.gson cannot be resolved to a module"
If I instead try "requires gson;" It works, but I get a warning.
"Name of automatic module 'gson' is unstable, it is derived from the module's file name."
But the file does compile without problems.
But if I try to run the project I get error...
"Error occurred during initialization of boot layer java.lang.module.FindException: Module gson not found, required by ..."
The above two errors suggest that Eclipse is having trouble interpreting gson-2.13.1.jar.
On the command line "jar --describe-module --file gson-2.11.0.jar"
Shows that this is a multi-release jar file with 9 releases in it.
It seems that for some reason Eclipse is having trouble seeing the "com.google.gson" module in the jar file. It works for some parts of Eclipse (like code completion), but not when checks for compile errors.
Eclipse was instead using the automatically derived module name "gson" based on the file name. But the java.exe was correctly processing the jar file but obviously couldn't find a module named "gson" because the real module name was "com.google.gson".
Since there was a mismatch I was getting the runtime error.
What I did
Given this, my hacky workaround was to rename "gson-2.13.1.jar" to "com.google.gson-2.13.1.jar"
This made the automatically derived package name and the acutal package name in the jar file's "module-info.class" to both be the same one ("com.google.gson"). After doing this I can both compile and run the program without problems. And the JSON parsing works as expected.
Question
Having to rename the jar file feels kind of wrong. I shouldn't have to be doing this.
Is this a bug in eclipse? Or is there something else configuration wise I should be doing here to make this work?
I have never had this kind of problem when including any other jar files in my projects.
This is unfortunately a known issue with Gson (or rather with Eclipse), see Gson issue #2679.
Eclipse IDE (and therefore also the VS Code extension "Language Support for Java(TM) by Red Hat") does not properly consider the module declaration in certain cases, see Eclipse issue #2495. Gson uses the tool ModiTect to add the module declaration, and that unfortunately made changes which triggered this bug now.
There is currently no solution to this, other than workarounds like the one you mentioned in your question. Some projects like Jackson have downgraded their ModiTect dependency due to this issue, but the disadvantage is that this then misses out on other ModiTect improvements and bug fixes.