Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8232939

"-encoding" errors do not fail compilation when using the JavaCompiler API

    Details

    • Type: Bug
    • Status: New
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 11.0.4
    • Fix Version/s: None
    • Component/s: tools
    • Labels:
    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      A DESCRIPTION OF THE PROBLEM :
      When invalid characters are encountered by javac according to the -encoding flag, errors are printed and compilation fails. When the same characters are encountered with JavaCompiler.getTask(...).call(), the errors are printed but the return value is true (i.e. success). This means that compilation that fails on the CLI can succeed in the API.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile a file containing non-ASCII characters while providing ["-encoding" "ASCII"] to the JavaCompiler. Print the results of invoking the compilation task.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The errors should be printed, followed by "false" for compilation failure.
      ACTUAL -
      The errors are printed followed by "true" for compilation success. A class file with the invalid characters replaced by U+FFFD is created.

      ---------- BEGIN SOURCE ----------
      package encoding.test;

      import java.util.List;
      import javax.tools.ToolProvider;
      import java.nio.charset.StandardCharsets;
      import java.nio.file.Path;

      public class Library {
          private final String foo = "да";

          public static void main(String[] args) {
              var compiler = ToolProvider.getSystemJavaCompiler();
              var fm = compiler.getStandardFileManager(null, null, StandardCharsets.US_ASCII);
              // Note that this is the path to this file; you will have to change it to match where this source code is
              // located on your system.
              var files = fm.getJavaFileObjects(Path.of("/tmp/encoding-test/src/main/java/encoding/test/Library.java"));
              System.out.println(compiler.getTask(null, fm, null, List.of("-encoding", "ASCII"), null, files).call());
          }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: