8210275: Source Launcher should fail if --source is used without a source file
authorjjg
Fri, 21 Sep 2018 15:38:43 -0700
changeset 51841 f191aca8f96d
parent 51840 dc15e45122b2
child 51842 b7153eff0558
8210275: Source Launcher should fail if --source is used without a source file Reviewed-by: mchung, alanb, mcimadamore
src/java.base/share/native/libjli/java.c
src/jdk.compiler/share/classes/com/sun/tools/javac/resources/launcher.properties
test/jdk/tools/launcher/SourceMode.java
test/langtools/tools/javac/launcher/SourceLauncherTest.java
--- a/src/java.base/share/native/libjli/java.c	Fri Sep 21 14:50:06 2018 -0700
+++ b/src/java.base/share/native/libjli/java.c	Fri Sep 21 15:38:43 2018 -0700
@@ -1326,7 +1326,9 @@
                    JLI_StrCmp(arg, "-cp") == 0) {
             REPORT_ERROR (has_arg_any_len, ARG_ERROR1, arg);
             SetClassPath(value);
-            mode = LM_CLASS;
+            if (mode != LM_SOURCE) {
+                mode = LM_CLASS;
+            }
         } else if (JLI_StrCmp(arg, "--list-modules") == 0) {
             listModules = JNI_TRUE;
         } else if (JLI_StrCmp(arg, "--show-resolved-modules") == 0) {
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/launcher.properties	Fri Sep 21 14:50:06 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/launcher.properties	Fri Sep 21 15:38:43 2018 -0700
@@ -82,21 +82,21 @@
     error:\u0020
 
 launcher.err.no.args=\
-    no filename
+    no path for source file
 
 # 0: string
 launcher.err.invalid.filename=\
-    invalid filename: {0}
+    invalid path for source file: {0}
 
 # 0: path
 launcher.err.file.not.found=\
-    file not found: {0}
+    source file not found: {0}
 
 launcher.err.compilation.failed=\
     compilation failed
 
 launcher.err.no.class=\
-    no class declared in file
+    no class declared in source file
 
 launcher.err.main.not.public.static=\
     ''main'' method is not declared ''public static''
@@ -122,7 +122,7 @@
 
 # 0: path, 1: object
 launcher.err.cant.read.file=\
-    error reading file {0}: {1}
+    error reading source file {0}: {1}
 
 # 0: string
 launcher.err.no.value.for.option=\
--- a/test/jdk/tools/launcher/SourceMode.java	Fri Sep 21 14:50:06 2018 -0700
+++ b/test/jdk/tools/launcher/SourceMode.java	Fri Sep 21 15:38:43 2018 -0700
@@ -23,7 +23,7 @@
 
 /**
  * @test
- * @bug 8192920 8204588
+ * @bug 8192920 8204588 8210275
  * @summary Test source mode
  * @modules jdk.compiler jdk.jlink
  * @run main SourceMode
@@ -251,6 +251,31 @@
         show(tr);
     }
 
+    // java --source N -cp ... HelloWorld
+    @Test
+    void testSourceClasspath() throws IOException {
+        starting("testSourceClasspath");
+        Path base = Files.createDirectories(Paths.get("testSourceClasspath"));
+        Path src = Files.createDirectories(base.resolve("src"));
+        Path srcfile = src.resolve("java.java");
+        createFile(srcfile, List.of(
+                "class HelloWorld {",
+                "    public static void main(String... args) {",
+                "        System.out.println(\"Hello World\");",
+                "    }",
+                "}"
+        ));
+        Path classes = base.resolve("classes");
+        compile("-d", classes.toString(), srcfile.toString());
+        TestResult tr =
+            doExec(javaCmd, "--source", thisVersion, "-cp", classes.toString(), "HelloWorld");
+        if (tr.isOK())
+            error(tr, "Command succeeded unexpectedly");
+        if (!tr.contains("file not found: HelloWorld"))
+            error(tr, "Expected output not found");
+        show(tr);
+    }
+
     // java --source
     @Test
     void testSourceNoArg() throws IOException {
--- a/test/langtools/tools/javac/launcher/SourceLauncherTest.java	Fri Sep 21 14:50:06 2018 -0700
+++ b/test/langtools/tools/javac/launcher/SourceLauncherTest.java	Fri Sep 21 15:38:43 2018 -0700
@@ -229,7 +229,7 @@
         Files.createDirectories(base);
         Path file = base.resolve("NoClass.java");
         Files.write(file, List.of("package p;"));
-        testError(file, "", "error: no class declared in file");
+        testError(file, "", "error: no class declared in source file");
     }
 
     @Test