同一个 javac 编译器是否可以编译相同的源文件集但生成不同校验和的类文件? [英] Is it possible for the same javac compiler to compile the same set of source files but produce class files of different checksums?

查看:25
本文介绍了同一个 javac 编译器是否可以编译相同的源文件集但生成不同校验和的类文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图比较这个结果:(在蚂蚁中)

I was attempting to compare the results of this: (in ant)

                       <javac 
                           target="1.5"   
                           source="1.5"
                           deprecation="on"
                           fork="yes"
                           optimize="true"
                           debug="true"
                           debuglevel="lines,vars,source">

                            <classpath>
                                <fileset dir="${project.basedir}/../lib">
                                    <include name="**/*.jar" />
                                    <include name="**/*.zip" />
                                </fileset>                        
                            </classpath>     
                        </javac>

...反对这个:(在maven中)

...against this: (in maven)

        <plugin>                
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <executions>
                <execution>
                    <id>compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>                
                    <configuration>
                        <target>1.5</target>
                        <source>1.5</source>
                        <debug>true</debug>
                        <debuglevel>lines,vars,source</debuglevel>
                        <showDeprecation>true</showDeprecation>
                        <optimize>true</optimize>
                        <fork>true</fork>
                        <includes>
                            ...
                        </includes>
                    </configuration>
                </execution>
            </executions>
        </plugin> 

...只是发现生成的 2 个类文件的校验和略有不同,而其余的则相同.我认为多线程发挥了作用,但为任一选项生成的校验和在重复尝试后似乎是一致的.什么可以解释这个结果?

...only to discover that 2 of the resulting class files have slightly different checksums, while the rest are identical. I consider multi-threading to have played a part, but the checksums produced for either option appear to be consistent upon repeated attempts. What can possibly account for this result?

更新:

我使用 javap -verbose 检查了一组具有不同校验和的文件,并注意到唯一的区别是:

I have examined one set of the files with different checksums using javap -verbose and noticed the only difference being:

const #16 = class   #108;   //  java/lang/Exception
const #17 = Method  #102.#109;  //  java/io/Writer.close:()V
const #18 = Method  #7.#109;    //  java/io/FileWriter.close:()V

相反:

const #16 = Method  #102.#108;  //  java/io/Writer.close:()V
const #17 = Method  #7.#108;    //  java/io/FileWriter.close:()V
const #18 = class   #109;   //  java/lang/Exception

推荐答案

如您所示,不同之处在于生成的常量池,这确实不是问题,但令人不安,因为您期望相同的输出具有相同的编译器和选项.我敢打赌,在这两种情况下,编译器会以不同的顺序调用 java 文件,并且编译的顺序会影响结果.

as you showed, the difference is in the generated constant pool, which is really a non-issue, but troubling as you would expect the same output with the same compiler and options. i would bet that the compiler is called with the java files in different order between the two situations, and the order of compilation is affecting the result.

这篇关于同一个 javac 编译器是否可以编译相同的源文件集但生成不同校验和的类文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆