Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build failure with Java 21 #2501

Closed
ebourg opened this issue Sep 29, 2023 · 11 comments · Fixed by #2747
Closed

Build failure with Java 21 #2501

ebourg opened this issue Sep 29, 2023 · 11 comments · Fixed by #2747
Labels
bug java8 Issues related to making Java 8 the minimum supported version

Comments

@ebourg
Copy link

ebourg commented Sep 29, 2023

Gson fails to build with Java 21, for two reasons:

  1. The source/target level 7 is no longer supported:

     [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project gson: Fatal error compiling: error: release version 7 not supported -> [Help 1]
    
  2. If the level is changed to 8, compiler warnings appear and break the build:

     [INFO] -------------------------------------------------------------
     [WARNING] COMPILATION WARNING :
     [INFO] -------------------------------------------------------------
     [WARNING] gson/src/main/java/com/google/gson/internal/$Gson$Types.java:[500,24] non-transient instance field of a serializable class declared with a non-serializable type
     [WARNING] gson/src/main/java/com/google/gson/internal/$Gson$Types.java:[501,24] non-transient instance field of a serializable class declared with a non-serializable type
     [WARNING] gson/src/main/java/com/google/gson/internal/$Gson$Types.java:[502,26] non-transient instance field of a serializable class declared with an array having a non-serializable base component type java.lang.reflect.Type
     [WARNING] gson/src/main/java/com/google/gson/internal/$Gson$Types.java:[567,24] non-transient instance field of a serializable class declared with a non-serializable type
     [WARNING] gson/src/main/java/com/google/gson/internal/$Gson$Types.java:[601,24] non-transient instance field of a serializable class declared with a non-serializable type
     [WARNING] gson/src/main/java/com/google/gson/internal/$Gson$Types.java:[602,24] non-transient instance field of a serializable class declared with a non-serializable type
     [INFO] 6 warnings
     [INFO] -------------------------------------------------------------
     [INFO] -------------------------------------------------------------
     [ERROR] COMPILATION ERROR :
     [INFO] -------------------------------------------------------------
     [ERROR] gson/src/main/java/com/google/gson/internal/$Gson$Types.java: warnings found and -Werror specified
     [INFO] 1 error
     [INFO] -------------------------------------------------------------
    
@ebourg ebourg added the bug label Sep 29, 2023
@eamonnmcmanus
Copy link
Member

Thanks for letting us know!

We are not likely to start building with Java 21 anytime soon, so for the purposes of the GitHub repo this isn't an immediate issue. On the other hand, the compilation warnings are easily suppressed with @SuppressWarnings("serial") so I don't see why we wouldn't add that. The fields in question are all of type Type, and the code goes out of its way to ensure that their values are always either Class (which is Serializable) or one of the nested Type implementations in $Gson$Types (which are also Serializable). Why they need to be serializable at all is not clear to me but it's probably not worth fiddling with that.

If you do suppress warnings, the build fails later anyway like this:

[[INFO] proguard jar: [/Users/emcmanus/.m2/repository/com/guardsquare/proguard-base/7.2.2/proguard-base-7.2.2.jar, /Users/emcmanus/.m2/repository/com/guardsquare/proguard-core/9.0.1/proguard-core-9.0.1.jar]
[proguard] ProGuard, version 7.2.2
[proguard] Unexpected error
[proguard] java.io.IOException: Can't read [/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home/jmods/java.base.jmod] (Can't process class [module-info.class] (Unsupported version number [65.0] (maximum 62.65535, Java 18)))

We're using the latest version of proguard-maven-plugin so that's probably a blocker for now. I see that the forthcoming ProGuard 7.4.0 will support Java 21, and presumably there'll be a new release of proguard-maven-plugin then.

I sent #2502 to suppress the serialization warnings.

@Marcono1234
Copy link
Collaborator

Besides the issue with ProGuard one of the tests is also failing:

[ERROR] com.google.gson.functional.ReflectionAccessTest.testRestrictiveSecurityManager -- Time elapsed: 0.024 s <<< ERROR!
java.lang.UnsupportedOperationException: The Security Manager is deprecated and will be removed in a future release
        at java.base/java.lang.System.setSecurityManager(System.java:429)
        at com.google.gson.functional.ReflectionAccessTest.testRestrictiveSecurityManager(ReflectionAccessTest.java:73)

The shrinker-test also seems to fail because R8 does not support Java 21 classfiles yet.

@denAbramoff
Copy link

denAbramoff commented Dec 26, 2023

With Spring 6.1 with maven-compiler-plugin 3.12 and compileArg -parameters i get error like this

[ERROR] bad class file: .m2/repository/com/google/code/gson/gson/2.10.1/gson-2.10.1.jar(/com/google/gson/JsonElement.class)
[ERROR] bad RuntimeInvisibleParameterAnnotations attribute: JsonElement()

@Marcono1234
Copy link
Collaborator

@denAbramoff, a few questions:

  • Is there any additional information printed?
  • Does this only occur with JDK 21, or also when you build the project (with the same build configuration) with JDK 17 or 11?
  • Does this happen with older Gson versions, especially older than 2.10 (since in that version the JsonElement() constructor got a @Deprecated annotation; though not sure if that had any effect)?
  • Can you please delete the gson-2.10.1.jar file from your local Maven repository (so that Maven downloads it again), in case it got corrupted for some reason?
  • Can you please create a small self contained example project which allows reproducing this?

@Marcono1234
Copy link
Collaborator

However, could you please create a separate GitHub issue, @denAbramoff?

This issue here is about building Gson itself (i.e. the source code of this repository) using JDK 21, and not about using Gson in a project which is built using JDK 21.

@denAbramoff
Copy link

Ok, i will check a few moments and do it

@Marcono1234
Copy link
Collaborator

Marcono1234 commented Dec 27, 2023

@ebourg, it should now be possible to build Gson with JDK 21 (see pull request #2552). The only limitation is as you mentioned that JDK 21 does not support Java 7 as target version, therefore building with JDK 21 currently intentionally requires using -Denforcer.fail=false (see the PR for more details), and the README says that building with JDK 21 is not officially supported (but using Gson with JDK 21 is).

Since changing the target version from 7 to something higher is a user-visible change, it does not seem to be justified to do this just to be able to build with JDK 21. There is however in other issues already the discussion about increasing the target version to Java 8.

Could you please double-check just to make sure you can build with JDK 21 now as well?

I guess we can keep this issue here open though to track the following remaining tasks once the target version is Java 8:

  • updating the README to mention that building with JDK 21 is supported
  • removing Maven profile jdk21+
  • adjusting Maven Enforcer configuration which prevents building with JDK 21, but maybe prevent JDK 22+?
    (and adjusting the GitHub workflow accordingly)

(effectively this undoes / adjusts some of the #2552 changes)

@seppaleinen
Copy link

If someones interested proguard-maven-plugin have released a new version 2.6.1 which integrates proguard 7.4.1. Seems to work with java21 now :)

@google google deleted a comment from nicolashutta Feb 26, 2024
@eamonnmcmanus eamonnmcmanus added the java8 Issues related to making Java 8 the minimum supported version label Mar 2, 2024
@Abhinavrajsrivastav
Copy link

[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.260 s
[INFO] Finished at: 2024-04-07T13:25:15+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project demo: Fatal error compiling: error: release version 21 not supported -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
PS C:\Users\91991\Desktop\hello\demo> any Idea about this , How can I fix this issue.

@Marcono1234
Copy link
Collaborator

@Abhinavrajsrivastav, that seems to be unrelated to Gson. It looks like you are specifying Java 21 as release version, but the JDK you are using is older.
Run mvn --version and it will print the JDK it is using and its file path. Either install a newer JDK, temporarily change your PATH and JAVA_HOME environment variables or try using Maven toolchains.

For any follow-up questions on this, please ask them on Stack Overflow or similar, unless they are directly related to Gson, to not move the discussion here too far off-topic.

@Abhinavrajsrivastav
Copy link

@Marcono1234 yes, I was using the latest java 21 version which was not compatible with the curent mvm version which is 3.8.6, I uodated jdk version to 17 and now it's completely working fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug java8 Issues related to making Java 8 the minimum supported version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants