Analysis and Solution of inconsistent compiling results between eclipse and javac

Source: Internet
Author: User

Problem:

The following is a simple class:

Public class mytest {
Private Static string classname = string. Class. getname (); // The red part is the key to the following issues:
Public static void main (string [] ARGs ){
System. Out. println (classname );
}
}

After eclipse3.1.1 is compiled (the compatibility version of the specified class is 1.4), The Decompilation result is:

Public class mytest
{

Public mytest ()
{
}

Public static void main (string ARGs [])
{
System. Out. println (classname );
}

Private Static string classname;

Static
{
Classname = java. Lang. String. Class. getname ();
}
}

After Sun javac (or ant, antx) is compiled (JDK 1.4 or jdk1.5, but the compilation result is specified as 1.4), The Decompilation result is:

Public class mytest
{

Public mytest ()
{
}

Public static void main (string ARGs [])
{
System. Out. println (classname );
}

Static class _ mthclass $ (string X0)
{
Return class. forname (x0 );
Classnotfoundexception x1;
X1;
Throw (New noclassdeffounderror (). initcause (X1 );
}

Private Static string classname;

Static
{
Classname = (Java. Lang. String. Class). getname ();
}
}

 

That is to say, the result compiled by Sun javac contains an extra _ mthclass $ method, which usually has no problem, however, this becomes a problem when Hot Swap technology is used (for example, the rapid deployment plug-in antx eclipse plugin uses Hot Swap to implement hot replacement of classes, or some class serialization areas.

 

With _ mthclass $ search on Google, the results are not much, more valuable is this article: http://coding.derkeiler.com/Archive/Java/comp.lang.java.softwaretools/2004-01/0138.html

According to this statement, this problem is caused by Sun's failure to comply with the specifications, while eclipse compiler follows the specifications, and eclipse compiler cannot be replaced.

 

If you try to replace the JDK version with 1.5, the _ mthclass $ generated by Sun javac will disappear. However, this strange time is that a member variable class $0 is added to the eclipse compilation result,
The following is the decompilation result after being compiled by eclipse3.1.1 (specifying the class compatibility version as 5.0:
Public class mytest
{

Public mytest ()
{
}

Public static void main (string ARGs [])
{
System. Out. println (classname );
}

Private Static string classname = Java/lang/string. getname ();
Static class $0;

}
After Sun javac (or ant, antx) is compiled (JDK 1.5), The Decompilation result is:
Public class mytest
{

Public mytest ()
{
}

Public static void main (string ARGs [])
{
System. Out. println (classname );
}

Private Static string classname = Java/lang/string. getname ();

}

I searched Goole and found that eclipse3.2 has two important features:
1. better compatibility with javac.
2. provides a separate compiler that can be used outside eclipse, including ant.

To download eclipse3.2, first verify its compatibility with sun javac,
When JDK is used, it is still the same as the old one. The _ mthclass $ method compiled by Sun javac does not exist in the compilation result of eclipse3.2, so it is not compatible.
However, when JDK 3.2 is used, the compilation result of eclipse is eventually consistent with that of Sun javac.

Although jdk1.5 and eclipse 3.2 have ensured the compatibility of this class in the two compilers, I always feel that it is not practical:
1. Who knows there is no other incompatibility between the two compilers?
2. The version requirements are too strict. It is difficult to use these versions for various reasons.

Therefore, it is more appropriate to fundamentally solve this problem: the best solution is not to use two different compilers, but to use the same one.
Since the compilers in the eclipse environment cannot be replaced, it is not feasible to use sun javac in all attempts. How can we use the built-in eclipse compiler in a unified manner?
The second important feature of eclipse3.2 just mentioned comes in handy.
The independent eclipse Compiler (1 MB size) can be downloaded at the address below: http://www.eclipse.org/downloads/download.php? File =/Eclipse/downloads/drops/R-3.2-200606291905/ECJ. Jar

The use of this independent compiler in antx is also very simple: (for the independent use of the compiler or the following use of ant, see this help section: jdt plug-in developer guide> programmer's guide> jdt core> compiling Java code)
1. Put the downloaded compiler under the antx_home/lib directory.
2. Add the following line to project. xml of the project file: <property name = "build. compiler" value = "org. Eclipse. jdt. Core. jdtcompileradapter"/>
This ensures that the classes packaged through antx are also compiled using the eclipse compiler. Of course, class incompatibility should not exist.

 

In fact, eclipse3.1.1 also provides an independent eclipse compiler, but at that time it did not provide a separate package for download. If you want to use the eclipse compiler of version 3.1.1, you can use Org. eclipse. jdt. core_3.1.1.jar and the jdtcompileradapter contained in it. jar. (I have not independently verified the compiler in eclipse3.1.1 environment)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.