Repository navigation
Java 25: Compact source files and instance main methods (JEP 512) - #107
Merged
Merged
Conversation
Test Results283 tests +1 280 ✅ +1 6s ⏱️ ±0s Results for commit 75fccf8. ± Comparison against base commit b42879b. This pull request skips 1 and un-skips 1 tests. |
mperor
marked this pull request as ready for review
October 30, 2026 07:19
|
@mperor ⏰ This daily topic is still waiting for your review. |
2 similar comments
|
@mperor ⏰ This daily topic is still waiting for your review. |
|
@mperor ⏰ This daily topic is still waiting for your review. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What you'll learn
void main()— the compiler synthesizes a final top-level class in the unnamed package around itjava CompactSourceFile.javajava.basemodule is implicitly imported, so types likejava.io.IOandjava.util.Listare usable without anyimportjavasource launcher and assert its captured outputWhy it matters
Compact source files lower the barrier for small scripts and first Java programs, but understanding why a test can't just call into them directly — the unnamed-package import restriction — is a useful, often-missed detail about how Java's package system actually works.
Read more
JEP 512: Compact Source Files and Instance Main Methods
Quiz
CompactSourceFile.javahas nopackagestatement. CanJava25.java(in packagepl.mperor.lab.java) addimport CompactSourceFile;and call it directly instead of launching it as a subprocess?Answer
No. A class with no package declaration lives in the unnamed package, and the Java Language Specification forbids any type in a *named* package from importing a type from the unnamed package (JLS §7.5.3). The only way to invoke it from a named-package test is to run it as a separate program — exactly what the source launcher (`java CompactSourceFile.java`) is for.Note on local verification: this environment only has JDK 21 installed, and the Gradle build requires JDK 25 via toolchain. JDK 25 auto-provisioning (foojay) and direct downloads from Adoptium/Corretto/java.net were all blocked by network policy here, so I could not run
./gradlewor the new test locally. I manually sanity-checked that the compact-source-file syntax itself is accepted byjava --enable-previewon JDK 21 (modulo theIO/Listauto-import, which is JDK 25-only and unavailable to test here). Opening as a draft; CI (JDK 25) will verify the actual test run.Closes #94