Skip to content

Java 25: Compact source files and instance main methods (JEP 512) - #107

Merged
mperor merged 1 commit into
masterfrom
DAILY/94-compact-source-file-source-launcher
Nov 1, 2026
Merged

mperor merged 1 commit into
masterfrom
DAILY/94-compact-source-file-source-launcher

Conversation

@mperor

@mperor mperor commented Oct 30, 2026 •

Copy link
Copy Markdown
Owner

What you'll learn

  • a compact source file has no class declaration, no package, and an instance void main() — the compiler synthesizes a final top-level class in the unnamed package around it
  • such a file is meant to be launched directly, one file, no build step: java CompactSourceFile.java
  • inside a compact source file (and under the source launcher in general), the whole java.base module is implicitly imported, so types like java.io.IO and java.util.List are usable without any import
  • because the file has no package declaration, it lives in the unnamed package — a named package can never import from the unnamed one, so the only way to exercise it from a JUnit test is to launch it as a separate process via the java source launcher and assert its captured output

Why 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.java has no package statement. Can Java25.java (in package pl.mperor.lab.java) add import 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 ./gradlew or the new test locally. I manually sanity-checked that the compact-source-file syntax itself is accepted by java --enable-preview on JDK 21 (modulo the IO/List auto-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

@mperor mperor added the daily Queued for the daily learning PR label Oct 30, 2026
@github-actions

Copy link
Copy Markdown

Test Results

283 tests  +1   280 ✅ +1   6s ⏱️ ±0s
 80 suites ±0     3 💤 ±0 
 80 files   ±0     0 ❌ ±0 

Results for commit 75fccf8. ± Comparison against base commit b42879b.

This pull request skips 1 and un-skips 1 tests.
pl.mperor.lab.java.design.pattern.creational.singleton.SingletonTest ‑ testSerializationSafe()
pl.mperor.lab.java.design.pattern.creational.singleton.SingletonTest ‑ testThreadSafe()

@mperor
mperor marked this pull request as ready for review October 30, 2026 07:19
@github-actions

Copy link
Copy Markdown

@mperor ⏰ This daily topic is still waiting for your review.

2 similar comments
@github-actions

Copy link
Copy Markdown

@mperor ⏰ This daily topic is still waiting for your review.

@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

@mperor ⏰ This daily topic is still waiting for your review.

@mperor
mperor merged commit 2e6e257 into master Nov 1, 2026
2 checks passed
@mperor
mperor deleted the DAILY/94-compact-source-file-source-launcher branch November 1, 2026 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

daily Queued for the daily learning PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Java 25: Compact source files and instance main methods (JEP 512)

2 participants