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

javac shouldn't silently change .jar files on the classpath

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Unresolved
    • Icon: P4 P4
    • 25
    • tools
    • None
    • behavioral
    • low
    • Hide
      While it is unlikely anyone relies on updating a JAR file as a side-effect of adding it to the class-path, it is possible.

      After this change, classfiles derived from sources file in JAR files on the classpath will be placed in the current working directory.

      Use of the `-d` option to specifying an output directory will mitigate the writing of any affected class-files to the current directory.
      Show
      While it is unlikely anyone relies on updating a JAR file as a side-effect of adding it to the class-path, it is possible. After this change, classfiles derived from sources file in JAR files on the classpath will be placed in the current working directory. Use of the `-d` option to specifying an output directory will mitigate the writing of any affected class-files to the current directory.
    • Other
    • Implementation

      Summary

      javac JAR files on the class-path no longer have updated class files written back to them.

      Problem

      Note: This issue only occurs when javac is run without either the -sourcepath or -d (output directory) options.

      If there is no -sourcepath specified, javac will also search the classpath for source files, and may use the source file instead of a corresponding classfile, depending on the timestamps and entry ordering. By default these source files are compiled all the way into the corresponding classfiles. When there is no -d specified, javac generally places the output classfiles next to the sourcesfile. As a consequence, javac may modify the JAR file on the classpath, writing a new classfile into it.

      This is problematic, because the JAR files on the classpath may be from authoritative sources.

      Solution

      When determining output class-file paths, in the absence of the -d option, check for whether a source file object comes from a JAR file, and (if so) avoid specifying the output class file as its sibling. The classfile will be written into the current working directory instead.

      This solution reverts behavior to that found in JDK 8 and earlier, including writing any affected class files into the current working directory. This behavior results in a loss of package/directory consistency and is unlikely to be useful, but it is the historical behavior.

      Specification

      No formal specification change, and behavior is being reverted to that of JDK 8.

            dabeaumo David Beaumont
            simonis Volker Simonis
            Jan Lahoda
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: