Skip to content

Keep the realm's own Package while it defines a class on Java 8 - #170

Merged
slachiewicz merged 1 commit into
masterfrom
own-package-on-java-8
Nov 27, 2026
Merged

slachiewicz merged 1 commit into
masterfrom
own-package-on-java-8

Conversation

@slachiewicz

Copy link
Copy Markdown
Member

On Java 8, URLClassLoader.defineClass and ClassLoader.definePackage call the overridable ClassLoader.getPackage(String) to find or verify the package they are defining, so the lookup added for imports and the parent made a realm adopt the imported realm's Package (with that jar's manifest attributes) instead of defining its own, made a sealed package in either jar fail with SecurityException: sealing violation, and made definePackage throw IllegalArgumentException for an imported package. While a class or package is being defined on a thread, every realm's getPackage(String) on that thread now answers as ClassLoader does, including when the base loader is itself a realm; Java 9 and later use the final getDefinedPackage there and are untouched. Lookups from code running in the realm (Package.getPackage, getPackages) still see packages reached through imports and the parent, and the import lookup is skipped when the realm already has the package. The new tests run on every JDK and fail only on Java 8 without the fix.

Java 8 calls the overridable getPackage(String) while it defines a class
or package, so answering from imports or the parent there swaps in a
foreign Package and its sealing. The thread-wide guard makes that path
answer as in the last release, and the import lookup is skipped when the
realm already has the package; do not remove the guard as redundant.
@slachiewicz slachiewicz added the bug Something isn't working label Nov 26, 2026
@slachiewicz
slachiewicz marked this pull request as ready for review November 27, 2026 05:38
@slachiewicz
slachiewicz merged commit 29bd362 into master Nov 27, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant