Quick revision
The short answer
Gson has no built-in adapter for java.time types, so for an Instant it falls back to its reflective adapter and tries to read the JDK class's private fields. Since Java 16, and strictly since Java 17, the JDK's internals are strongly encapsulated, so making java.time.Instant's private fields accessible is refused with InaccessibleObjectException. The right fix is a TypeAdapter for Instant, for example writing an ISO-8601 string, registered on the GsonBuilder; adding --add-opens only hides the problem and ties our JSON format to JDK internals.
What to remember
In this answer, we saw that Gson has no adapter for java.time types, so it reflects into Instant's private fields, which strong encapsulation in Java 16 and 17 refuses with InaccessibleObjectException. --add-opens brings the old behaviour back but exposes JDK internals as our JSON format. A TypeAdapter that writes Instant as an ISO-8601 string, registered on the shared GsonBuilder, is the right fix, and reflection access filters in newer Gson versions make similar problems fail early and clearly.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Gson has no built-in adapter for java.time types, so for an Instant it falls back to its reflective adapter and tries to read the JDK class's private fields. Since Java 16, and strictly since Java 17, the JDK's internals are strongly encapsulated, so making java.time.Instant's private fields accessible is refused with InaccessibleObjectException. The right fix is a TypeAdapter for Instant, for example writing an ISO-8601 string, registered on the GsonBuilder; adding --add-opens only hides the problem and ties our JSON format to JDK internals.
Why this question is asked
Many services ran on Java 8 or 11 for years, and moving them to Java 17 or 21 or 25 is often the first time this error appears. It is confusing because the code has not changed and worked before. Interviewers ask about it because the fix shows whether we understand both how Gson serializes unknown types and what strong encapsulation in the JDK does. In this answer, we will recap Gson's fallback for unknown types, see what the module system changed, look at the tempting workaround and why it is wrong, write the proper TypeAdapter, and see how newer Gson versions make this fail earlier and more clearly.
1A Quick Recap of How Gson Handles an Unknown Type
For each type it serializes, Gson looks for a type adapter. It has adapters for primitives, strings, collections, maps, java.util.Date and a few others. java.time arrived in Java 8 and Gson never added built-in adapters for it. So when Gson meets an Instant, it treats it like any of our own classes and builds a reflective adapter, which reads the object's declared fields directly. Instant has two private fields, seconds and nanos.
On Java 8, nothing stopped that. Gson called setAccessible(true) on the private fields and wrote {"seconds":1762337730,"nanos":0}. The JSON was ugly and depended on how the JDK happened to implement Instant, but it worked, so nobody noticed.
2What Java 16 and 17 Changed
The Java module system, added in Java 9, divides the JDK into modules such as java.base. A module decides which packages it exports, meaning public types are usable, and which it opens, meaning reflection may also reach private members. java.base exports java.time but does not open it.
For a few releases the JDK allowed illegal reflective access with a warning. Java 16 switched the default to deny, and Java 17 removed the option to turn it back on, so JDK internals are now strongly encapsulated. Reflection on a private field of Instant from our code is refused.
6 messages in order. Play them, or step through them one at a time.
The fix is to stop step 2 from happening by giving Gson a real adapter for Instant.
3The Tempting Workaround and Why It Is Wrong
The error message itself suggests the workaround: start the JVM with --add-opens java.base/java.time=ALL-UNNAMED, which opens the package to all code on the classpath. The error goes away and the old output comes back.
But think about what we have just done. Our JSON format is now the private field layout of a JDK class, {"seconds":...,"nanos":...}, which no client should depend on and which the JDK may change at any release. We have weakened the protection the JDK added for good reasons, for every library in the process, not just Gson. And every new java.time type, LocalDate, Duration, ZonedDateTime, will hit the same wall later. The flag treats the symptom and leaves the real cause, a missing adapter, in place.
4The Right Fix: A TypeAdapter for Instant
A TypeAdapter tells Gson exactly how to write and read one type. For Instant, the natural JSON form is the ISO-8601 string, which Instant.toString() produces and Instant.parse() reads.
Now Gson never reflects into Instant, so encapsulation is no longer a concern, and the JSON format is one we chose and documented. The adapter is streaming, so it costs almost nothing per value. We add similar adapters for the other java.time types we use, LocalDate as yyyy-MM-dd for example, in the one shared Gson instance.
There is a catch when migrating: clients that consumed the old {"seconds":...} shape will break. If such clients exist, the adapter's read method can accept both shapes for a while, and the change of output should be announced like any other API change.
5Failing Fast With Reflection Access Filters
Newer Gson versions help catch this class of problem early. GsonBuilder.addReflectionAccessFilter lets us forbid reflection on certain classes, and ReflectionAccessFilter.BLOCK_INACCESSIBLE_JAVA or BLOCK_ALL_JAVA blocks reflection on JDK classes, so Gson throws a clear error naming the type that needs an adapter instead of failing deep inside the module system. These filters arrived in the 2.9 line and later releases improved the messages for JDK types; check your version.
Using BLOCK_ALL_JAVA in tests is a good habit, because it turns every silent reliance on JDK internals into a failing test, even on an older JDK where the reflection would still succeed.
6Follow-up questions
Why does java.util.Date work while Instant does not?
Gson ships a built-in adapter for java.util.Date (and java.sql date types), so it never needs reflection for them. java.time types have no built-in adapter, so they fall through to the reflective path that the module system now blocks.
Is there a library we can add instead of writing adapters?
There are community libraries that provide Gson adapters for java.time, and some teams use them. Writing the few adapters we need is also very little code and keeps the format under our control. In a Spring Boot service, switching to Jackson, whose JavaTimeModule is registered automatically, is often the better long-term answer.
Would --add-opens ever be acceptable?
Only as a short emergency measure to restore service while the adapter is written and released, with a ticket to remove it. It should never be the permanent fix, because it keeps the JSON tied to JDK internals.
7Conclusion
In this answer, we saw that Gson has no adapter for java.time types, so it reflects into Instant's private fields, which strong encapsulation in Java 16 and 17 refuses with InaccessibleObjectException. --add-opens brings the old behaviour back but exposes JDK internals as our JSON format. A TypeAdapter that writes Instant as an ISO-8601 string, registered on the shared GsonBuilder, is the right fix, and reflection access filters in newer Gson versions make similar problems fail early and clearly.
Open with
The cause is that Gson has no built-in adapter for java.time types. When it meets an Instant, it treats it like any user class and builds a reflective adapter that tries to read Instant's private fields, seconds and nanos.
Then, if they want more
On Java 8 that worked and produced an ugly JSON object, but since Java 16, and definitively since Java 17, the JDK packages are strongly encapsulated: java.base exports java.time but does not open it, so setAccessible on its private fields throws InaccessibleObjectException.
The workaround people reach for is --add-opens java.base/java.time, but that keeps our JSON format tied to JDK internals that can change, weakens encapsulation for the whole process, and the next java.time type will fail the same way.
The right fix is a TypeAdapter for Instant that writes value.toString(), the ISO-8601 string, and reads it back with Instant.parse, handling nulls, registered once on the shared GsonBuilder, with similar adapters for LocalDate and the other types we use. If old clients rely on the seconds and nanos shape, I would accept both on reading during a transition.
Newer Gson versions also offer reflection access filters such as BLOCK_ALL_JAVA, which I would enable at least in tests so this fails early with a clear message.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 17, Java 8, Java 16, Java 9
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A service upgraded to Java 17 and now Gson throws InaccessibleObjectException when...": https://scrivio-interview-library.netlify.app/java/json-libraries/567f4f8e43/