package com.google.gson.internal.bind.util is declared in module com.google.gson, which does not export it

142 Views Asked by At

I got this error message when compiling code generated from the OpenAPI generator:

package com.google.gson.internal.bind.util is declared in module com.google.gson, which does not export it

In my build.gradle I used this dependency:

implementation 'com.google.code.gson:gson:2.10.1'

and in my module-info.java I included:

requires com.google.gson;
2

There are 2 best solutions below

0
David Miller On

In build.gradle include "--add-exports $MODULE/$PACKAGE=$READING_MODULE":

compileJava {
doFirst {
    options.compilerArgs = [
            '--module-path', classpath.asPath,                
            '--add-exports', 'com.google.gson/com.google.gson.internal.bind.util=MyModule' // resolves "package com.google.gson.internal.bind.util is declared in module com.google.gson, which does not export it" 
    ]
    classpath = files()                                            
}

}

2
Marcono1234 On

While adding compiler options to allow access to this package solves the error, the question is rather why you rely on internal Gson packages in the first place, and if there are official alternatives to this.

Based on the package name it sounds like you are using Gson's internal ISO8601Utils class. A better alternative to this would be to use the classes from the java.time package or the java.time.format.DateTimeFormatter class for formatting and parsing dates.