是否有可能为同一javac编译器来编译同一套源文件而产生不同的校验的类文件? [英] Is it possible for the same javac compiler to compile the same set of source files but produce class files of different checksums?

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

问题描述

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

 < javac的
                           目标=1.5
                           来源=1.5
                           德precation =上
                           叉=YES
                           优化=真
                           调试=真
                           DEBUGLEVEL =行,增值经销商,源>                            <&类路径GT;
                                <文件集DIR =$ {} project.basedir LIB /../>
                                    <包括姓名=** / *罐子。/>
                                    <包括姓名=** / *拉链。/>
                                < /文件集>
                            < /类路径>
                        < / javac的>

...反对这种(在Maven中)

 <&插件GT;
            <&的artifactId GT; Maven的编译器插件< / artifactId的>
            <&版GT; 2.3.2< /版本>
            <&执行GT;
                <执行与GT;
                    <&ID GT;&编译LT; / ID>
                    <阶段>&编译LT; /阶段>
                    <目标>
                        <&目标GT;&编译LT; /目标>
                    < /目标>
                    <结构>
                        <目标> 1.5 LT; /目标与GT;
                        <信源> 1.5 LT; /源>
                        <调试和GT;真< /调试>
                        <&DEBUGLEVEL GT;线,增值经销商,源< / DEBUGLEVEL>
                        < showDe precation>真< / showDe precation>
                        <优化>真< /优化>
                        <&叉GT;真< /前叉>
                        &所述包括(includes)GT;
                            ...
                        < / - 包括GT;
                    < /结构>
                < /执行>
            < /处决>
        < /插件>

...才发现,所产生的类文件2具有略微不同的校验和,而其余的都是相同的。我认为多线程已经发挥了作用,但对于任一选项产生的校验似乎是在重复尝试是一致的。什么都不可能解释这一结果呢?

更新:

我已经检查一组使用不同的校验文件的的javap -verbose ,发现唯一的区别是:

 常量#16 =#类108; //爪哇/郎/异常
常量#17 =方法#102#109; //的Java / IO / Writer.close :()V
常量#18 =方法#7#109; //的Java / IO / FileWriter.close :()V

,而不是

 常量#16 =方法#102#108; //的Java / IO / Writer.close :()V
常量#17 =方法#7#108; //的Java / IO / FileWriter.close :()V
常量#18 =#类109; //爪哇/郎/异常


解决方案

为您呈现,不同的是在生成的常量池,这实在是一个不是问题的问题,但是麻烦你所期望的一样相同的输出编译器和选项。我敢打赌,编译器调用在两种情况之间不同的顺序java文件和编译的顺序影响结果。

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>

...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> 

...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?

Update:

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

as opposed to:

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

解决方案

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天全站免登陆