diff --git a/JavaReleases/src/main/java/pl/mperor/lab/java/CompactSourceFile.java b/JavaReleases/src/main/java/pl/mperor/lab/java/CompactSourceFile.java index 73108be..ac1ac25 100644 --- a/JavaReleases/src/main/java/pl/mperor/lab/java/CompactSourceFile.java +++ b/JavaReleases/src/main/java/pl/mperor/lab/java/CompactSourceFile.java @@ -4,5 +4,6 @@ /// Has, as its members, the fields and methods in the compact source file; and /// Must have a launchable main method; if it does not, a compile-time error is reported. void main() { - IO.println("Compact source file & main method here ..."); + IO.println("Compact source file & main method here ..."); // IO (java.io) needs no import + IO.println("Items: " + List.of("a", "b", "c")); // List (java.util) needs no import either } \ No newline at end of file diff --git a/JavaReleases/src/test/java/pl/mperor/lab/java/Java25.java b/JavaReleases/src/test/java/pl/mperor/lab/java/Java25.java index b2e3a26..0786367 100644 --- a/JavaReleases/src/test/java/pl/mperor/lab/java/Java25.java +++ b/JavaReleases/src/test/java/pl/mperor/lab/java/Java25.java @@ -4,6 +4,8 @@ import org.junit.jupiter.api.Test; import pl.mperor.lab.common.TestUtils; +import java.io.IOException; +import java.nio.file.Path; import java.util.NoSuchElementException; /// Java 25 (September 2025) @@ -164,4 +166,25 @@ public void testModuleImportAmbiguityResolvedBySingleTypeImport() { Assertions.assertEquals(java.util.List.of("John Doe", "Jane Doe"), demo.names); } + /// CompactSourceFile.java has no package declaration, so it lives in the unnamed package and + /// cannot be imported here (a named package can never import from the unnamed one). It is + /// launched the way JEP 512 intends: directly by the `java` source launcher, one file, no build step. + @Test + public void testCompactSourceFileRunsDirectlyViaSourceLauncher() throws IOException, InterruptedException { + var javaLauncher = Path.of(System.getProperty("java.home"), "bin", "java"); + var sourceFile = Path.of("src", "main", "java", "pl", "mperor", "lab", "java", "CompactSourceFile.java"); + + var process = new ProcessBuilder(javaLauncher.toString(), sourceFile.toString()) + .redirectErrorStream(true) + .start(); + String output = new String(process.getInputStream().readAllBytes()); + int exitCode = process.waitFor(); + + Assertions.assertEquals(0, exitCode, output); + Assertions.assertEquals( + "Compact source file & main method here ..." + System.lineSeparator() + + "Items: [a, b, c]" + System.lineSeparator(), + output); + } + }