Skip to content

Make URLJarCollector tolerant of JDK 21 (and newer JVMs)#90

Merged
pavelsavara merged 2 commits into
robo-code:mainfrom
pavelsavara:jdk_21
Jun 3, 2026
Merged

Make URLJarCollector tolerant of JDK 21 (and newer JVMs)#90
pavelsavara merged 2 commits into
robo-code:mainfrom
pavelsavara:jdk_21

Conversation

@pavelsavara

Copy link
Copy Markdown
Member

Summary

This change hardens the static initializer in
robocode.core/src/main/java/net/sf/robocode/io/URLJarCollector.java so Robocode
runs correctly on JDK 21 and other modern JVMs where the internal
sun.net.www.protocol.jar.* layout differs from the classic Sun/Oracle JVM.

Problem

URLJarCollector reflects into the JDK-internal classes
sun.net.www.protocol.jar.JarFileFactory and
sun.net.www.protocol.jar.JarURLConnection to enable a fast, caching JAR
connection path. On JDK 21:

  • The reflective access into these internal classes can fail with a
    RuntimeException (e.g. InaccessibleObjectException from the module system),
    which was not caught by the previous catch clause.
  • Even when the fields resolve, fileCache / urlCache may not be in the shape
    the caching path assumes.

Because the previous code only caught the checked reflection exceptions
(ClassNotFoundException | NoSuchFieldException | IllegalAccessException), any
RuntimeException thrown during initialization escaped the static block and
prevented the class from loading.

Fix

  • Treat the JVM as a "classic Sun/Oracle JVM" (sunJVM = true) only when both
    fileCache and urlCache were successfully resolved to non-null values,
    rather than assuming success after the reflection calls.
  • Also catch RuntimeException so unexpected failures (such as the module-system
    access errors seen on JDK 21) gracefully fall back to the non-caching code path
    instead of failing class initialization.

When the JVM is not recognized, openConnection simply disables URL caching
(urlConnection.setUseCaches(false)), which is the correct, portable behavior.

@pavelsavara pavelsavara self-assigned this Jun 2, 2026
@pavelsavara pavelsavara marked this pull request as ready for review June 3, 2026 19:15
@pavelsavara pavelsavara merged commit 172887e into robo-code:main Jun 3, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants