diff --git a/src/java.base/share/man/java.1 b/src/java.base/share/man/java.1 index d386fb15b8f..6499d70f989 100644 --- a/src/java.base/share/man/java.1 +++ b/src/java.base/share/man/java.1 @@ -279,6 +279,11 @@ the compilation. This sets both the source version accepted by compiler and the system API that may be used by the code in the source file. .IP \[bu] 2 +If \f[V]--enable-preview\f[R] is specified, the \f[V]--source N\f[R] +arguments can be omitted. +If the Java runtime version is \f[V]N\f[R], then \f[V]--release N\f[R] +is implied when compiling source files. +.IP \[bu] 2 If a \f[V]module-info.java\f[R] file exists in the \f[V]<source-root>\f[R] directory, its module declaration is used to define a named module that will contain all the classes compiled from diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/RelevantJavacOptions.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/RelevantJavacOptions.java index 88d1c875bc5..f619807f9bf 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/RelevantJavacOptions.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/RelevantJavacOptions.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -112,7 +112,9 @@ record RelevantJavacOptions(List<String> forProgramCompilation, programOptions.add(opt); subsequentOptions.add(opt); if (sourceOpt == null) { - throw new Fault(Errors.EnablePreviewRequiresSource); + String feature = String.valueOf(Runtime.version().feature()); + programOptions.addAll(List.of("--release", feature)); + subsequentOptions.addAll(List.of("--release", feature)); } } default -> { diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/launcher.properties b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/launcher.properties index 134d2ccd839..1bf209a7fd6 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/launcher.properties +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/launcher.properties @@ -1,5 +1,5 @@ # -# Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved. # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. # # This code is free software; you can redistribute it and/or modify it @@ -139,9 +139,6 @@ launcher.err.no.value.for.option=\ launcher.err.invalid.value.for.source=\ invalid value for --source option: {0} -launcher.err.enable.preview.requires.source=\ - --enable-preview must be used with --source - launcher.err.unnamed.pkg.not.allowed.named.modules=\ unnamed package is not allowed in named modules diff --git a/test/langtools/tools/javac/launcher/BasicSourceLauncherTests.java b/test/langtools/tools/javac/launcher/BasicSourceLauncherTests.java index 1a686a8a38f..bf44a704b31 100644 --- a/test/langtools/tools/javac/launcher/BasicSourceLauncherTests.java +++ b/test/langtools/tools/javac/launcher/BasicSourceLauncherTests.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -100,11 +100,7 @@ class BasicSourceLauncherTests { } """); - // Replace with plain Run.of(hi) once implict classes are out of preview - System.setProperty("jdk.internal.javac.source", String.valueOf(Runtime.version().feature())); var run = Run.of(hi, List.of("--enable-preview"), List.of()); - System.clearProperty("jdk.internal.javac.source"); - assertAll("# " + run, () -> assertLinesMatch( """ diff --git a/test/langtools/tools/javac/launcher/SourceLauncherTest.java b/test/langtools/tools/javac/launcher/SourceLauncherTest.java index a054415cfe1..cfa6c8ec9e5 100644 --- a/test/langtools/tools/javac/launcher/SourceLauncherTest.java +++ b/test/langtools/tools/javac/launcher/SourceLauncherTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -51,12 +51,9 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; -import java.util.Map; import java.util.List; import java.util.Properties; import java.util.regex.Pattern; -import java.util.stream.Collectors; import com.sun.tools.javac.launcher.SourceLauncher; import com.sun.tools.javac.launcher.Fault; @@ -534,10 +531,9 @@ public class SourceLauncherTest extends TestRunner { List<String> log = new JavaTask(tb) .vmOptions("--enable-preview") .className(base.resolve("HelloWorld.java").toString()) - .run(Task.Expect.FAIL) - .getOutputLines(Task.OutputKind.STDERR); - log = log.stream().filter(s->!s.matches("^Picked up .*JAVA.*OPTIONS:.*")).collect(Collectors.toList()); - checkEqual("stderr", log, List.of("error: --enable-preview must be used with --source")); + .run(Task.Expect.SUCCESS) + .getOutputLines(Task.OutputKind.STDOUT); + checkEqual("stdout", log, List.of("Hello World! []")); } @Test