Is the creation of Java class files deterministic?

前端 未结 11 1539
抹茶落季
抹茶落季 2020-12-02 11:04

When using the same JDK (i.e. the same javac executable), are the generated class files always identical? Can there be a difference depending on the

相关标签:
11条回答
  • 2020-12-02 11:39

    Short Answer - NO


    Long Answer

    They bytecode need not be the same for different platform. It's the JRE (Java Runtime Environment) which know how exactly to execute the bytecode.

    If you go through the Java VM specification you'll come to know that this needs not to be true that the bytecode is same for different platforms.

    Going through the class file format, it shows the structure of a class file as

    ClassFile {
        u4 magic;
        u2 minor_version;
        u2 major_version;
        u2 constant_pool_count;
        cp_info constant_pool[constant_pool_count-1];
        u2 access_flags;
        u2 this_class;
        u2 super_class;
        u2 interfaces_count;
        u2 interfaces[interfaces_count];
        u2 fields_count;
        field_info fields[fields_count];
        u2 methods_count;
        method_info methods[methods_count];
        u2 attributes_count;
        attribute_info attributes[attributes_count];
    }
    

    Checking about the minor and major version

    minor_version, major_version

    The values of the minor_version and major_version items are the minor and major version numbers of this class file.Together, a major and a minor version number determine the version of the class file format. If a class file has major version number M and minor version number m, we denote the version of its class file format as M.m. Thus, class file format versions may be ordered lexicographically, for example, 1.5 < 2.0 < 2.1. A Java virtual machine implementation can support a class file format of version v if and only if v lies in some contiguous range Mi.0 v Mj.m. Only Sun can specify what range of versions a Java virtual machine implementation conforming to a certain release level of the Java platform may support.1

    Reading more through the footnotes

    1 The Java virtual machine implementation of Sun's JDK release 1.0.2 supports class file format versions 45.0 through 45.3 inclusive. Sun's JDK releases 1.1.X can support class file formats of versions in the range 45.0 through 45.65535 inclusive. Implementations of version 1.2 of the Java 2 platform can support class file formats of versions in the range 45.0 through 46.0 inclusive.

    So, investigating all this shows that the class files generated on different platforms need not be identical.

    0 讨论(0)
  • 2020-12-02 11:41

    For the question:

    "What are the circumstances where the same javac executable,when run on a different platform, will produce different bytecode?"

    The Cross-Compilation example shows how we can use the Javac option:-target version

    This flag generates class files which are compatible with the Java version we specify while invoking this command. Hence the class files will differ depending on the attributes we supply during the compaliation using this option.

    0 讨论(0)
  • 2020-12-02 11:42

    Firstly, there's absolutely no such guarantee in the spec. A conforming compiler could stamp the time of compilation into the generated class file as an additional (custom) attribute, and the class file would still be correct. It would however produce a byte-level different file on every single build, and trivially so.

    Secondly, even without such nasty tricks about, there's no reason to expect a compiler to do exactly the same thing twice in a row unless both its configuration and its input are identical in the two cases. The spec does describe the source filename as one of the standard attributes, and adding blank lines to the source file could well change the line number table.

    Thirdly, I've never encountered any difference in build due to the host platform (other than that which was attributable to differences in what was on the classpath). The code which would vary based on platform (i.e., native code libraries) isn't part of the class file, and the actual generation of native code from the bytecode happens after the class is loaded.

    Fourthly (and most importantly) it reeks of a bad process smell (like a code smell, but for how you act on the code) to want to know this. Version the source if possible, not the build, and if you do need to version the build, version at the whole-component level and not on individual class files. For preference, use a CI server (such as Jenkins) to manage the process of turning source into runnable code.

    0 讨论(0)
  • 2020-12-02 11:46

    Overall, I'd have to say there is no guarantee that the same source will produce the same bytecode when compiled by the same compiler but on a different platform.

    I'd look into scenarios involving different languages (code-pages), for example Windows with Japanese language support. Think multi-byte characters; unless the compiler always assumes it needs to support all languages it might optimize for 8-bit ASCII.

    There is a section on binary compatibility in the Java Language Specification.

    Within the framework of Release-to-Release Binary Compatibility in SOM (Forman, Conner, Danforth, and Raper, Proceedings of OOPSLA '95), Java programming language binaries are binary compatible under all relevant transformations that the authors identify (with some caveats with respect to the addition of instance variables). Using their scheme, here is a list of some important binary compatible changes that the Java programming language supports:

    •Reimplementing existing methods, constructors, and initializers to improve performance.

    •Changing methods or constructors to return values on inputs for which they previously either threw exceptions that normally should not occur or failed by going into an infinite loop or causing a deadlock.

    •Adding new fields, methods, or constructors to an existing class or interface.

    •Deleting private fields, methods, or constructors of a class.

    •When an entire package is updated, deleting default (package-only) access fields, methods, or constructors of classes and interfaces in the package.

    •Reordering the fields, methods, or constructors in an existing type declaration.

    •Moving a method upward in the class hierarchy.

    •Reordering the list of direct superinterfaces of a class or interface.

    •Inserting new class or interface types in the type hierarchy.

    This chapter specifies minimum standards for binary compatibility guaranteed by all implementations. The Java programming language guarantees compatibility when binaries of classes and interfaces are mixed that are not known to be from compatible sources, but whose sources have been modified in the compatible ways described here. Note that we are discussing compatibility between releases of an application. A discussion of compatibility among releases of the Java SE platform is beyond the scope of this chapter.

    0 讨论(0)
  • 2020-12-02 11:46

    Java allows you write/compile code on one platform and run on different platform. AFAIK; this will be possible only when class file generated on different platform is same or technically same i.e. identical.

    Edit

    What i mean by technically same comment is that. They don't need to be exactly same if you compare byte by byte.

    So as per specification .class file of a class on different platforms don't need to match byte-by-byte.

    0 讨论(0)
提交回复
热议问题