Research on Java class version

Source: Internet
Author: User
Tags unsupported



I. Problems to be Solved




When we were an early adopter of jdk1.5, I believe many people have encountered unsupported Major. Minor version 49.0.
Wrong, at that time will be at a loss. At the beginning, there were not many Chinese documents related to this issue on the Internet. Now, you can find out how to solve this problem on the Internet. Most of them will tell you how to use JDK.
1.4 Re-compile. So why is that major. Minor? This is what we will talk about in this article to make the crowdsourced Security Testing come true.

I think I am more
Fortunately, I have studied the second version of deep Java Virtual Machine before the error. The original English book is "inside the Java Virtual Machine ".
Machine (second edition), you know where major. Minor is hiding, but you don't know it.
Unsupported Major. Minor version 49.0 will really interview, just to verify the fact for me.

First, we need
The direct sense of unsupported Major. Minor version 49.0 is that the classes compiled by jdk1.5 cannot be in JVM 1.4.
And must be compiled into a class that can be run under JVM 1.4. (Of course, if you are still using JVM 1.3 or JVM 1.2, You need to compile it into the target JVM.
Can be recognized ). This also resolves the problem.

Ii. Major. Minor
Where to live

What is major. Minor and where is it? First understand and find
Major. Minor.

Write a Java Hello world! Code, and then use JDK 1.5
Compiled into, helloworld. Java

Source "href =" http://www.blogjava.net/Unmi/archive/2007/12/04/165035.html#viewSource "> View
Source

Print
?01.
package
com.unmi;

02.
 
03.
public
class
HelloWorld

Alt2 ">04.
{

05.
    
public
static
void
main(String[]
args)


06.
    
{

07.
        
System.out.println(
"Hello,
World!"

);

08.
    
}

09.
}

Use
The bytecode helloworld. Class compiled by javac-D. helloworld. Java of JDK 1.5 is used
Content exposed by ultraedit:

What is major. Minor version?
It is equivalent to the Primary and Secondary version of a software, but here it is the primary and secondary version of a Java class identified. At the same time, we can see that minor_version is
0x0000, major_version is 0x0031, and the number of decimal systems is 0 and 49, respectively. That is, Major. Minor is 49.0.

3. What is major. Minor and what is it




Class
The 5th-8 bytes of the file are minor_version and major_version. Java class
New features may be added to the file format. Once the class file format changes, the version number also changes. For JVM, the version number identifies a specific class.
File Format. Generally, the JVM can read class files only after the primary version number and a series of minor version numbers are specified. If the version number of the class file exceeds JVM
The valid range that can be processed. JVM will not process the class file.

In Sun's JDK 1.0.2 release, JVM implementation supports
The format of the class file from 45.0 to 45.3. JVM in all JDK 1.1 releases can support versions from 45.0 to 45.65535
Class file format. In Sun SDK 1.2, JVM supports the class file format from version 45.0 to 46.0.

1.0
Or the compiler of version 1.2 can generate a class file with version 45.3. In Sun's 1.2 SDK, javac
By default, the compiler generates a class file with the version 45.3. However, if the-Target 1.2 flag is specified in the javac command line
The version compiler will generate a class file with the version 46.0. -Target 1.0 cannot be run on JVM 1.1 or 1.2
Mark generated by the class file.

In the second version of JVM implementation
Description of the main version number and secondary version number of the file. For the second version, the main version number of the class file must be consistent with that of the main release version of the Java platform (for example, in Java 2
The major version number increases from 45 to 46). The minor version number is related to each release version of a specific major platform. Therefore, although different classes
The file format can be expressed by different version numbers, but different version numbers do not represent different class file formats. The reason for different version numbers may be that the class file is released by different versions.
The format of the class file is not changed.

The above three paragraphs are excerpted from deep Java virtual machine.
We started the Java 2 era, but it was still far away from us. Many of us jumped directly to JDK 1.4.
On, I am similar, but the project requirement has to be grievance on JDK 1.3 for a period of time. However, the general information we can get is compiled by the JDK compiler of each version.
The class file contains a version number, and different JVMs can accept a range class version number. If the range is exceeded, an error occurs. However, they are generally backward compatible, knowing sun
Is it a Solaris slogan? Maintaining binary compatibility with the previous version 100% is also an investment protection for customers.

IV. Other methods for determining the Major. Minor version of the class



1) In eclipse
View
Eclipse 3.3
New features added: When a class is not associated with the source code, opening it will display more detailed class information, of course, it is not yet at the source code level, it is to open 2.0 spring. Jar
Information displayed by classpathxmlapplicationcontext. Class

2) command javap-verbose

For compiled class files, use javap-verbose to display the Major. Minor version of the class. See:

3) manifest File
There will be files in the jar package compiled by class.
META-INF/manifest, this file usually has compiler information, the following list of several packages of META-INF/manifest file content for everyone to see

· Content in the META-INFO/manifest section of velocity-1.5.jar
Manifest-version:
1.0
Ant-version: Apache ant 1.7.0
Created-:
Apache ant
Package: org. Apache. Velocity
Build-JDK: 1.4.2 _ 08


Extension-Name:
Velocity
We can see that it is packaged with ant, the JDK used for building is 1.4.2 _ 08, and the class compiled with 1.4 is in 1.4
Of course, it can run in JVM. It would be boring to use JDK 1.5 and ant 1.4 for compiling.
· 2. 0
META-INFO/manifest of spring. Jar
Manifest-version:
1.0
Ant-version: Apache ant 1.6.5
Created-by: 1.5.0 _ 08-b03 (Sun Microsystems Inc .)


Implementation-title:
Spring framework
Note that it is compiled with JDK 1.5, so does it contain-target?
1.4 or-target 1.3 for compilation? Indeed, you can view binary files of the class, which is the safest. The spring-2.0.jar can also be in
1.4 load and execute in JVM.
· META-INFO/manifest of jar packages using ant in one project
Manifest-version:
1.0
Ant-version: Apache ant 1.7.0
Created-by: 1.4.2-b28 (Sun Microsystems Inc .)



Build and package with JDK 1.4.

The first and second methods can clearly understand Major. Minor
Version, and the third method should be no problem, but encountered abnormal build is hard to say, such as who put that META-INFO/manifest
It is unknown that the package is changed. The method of directly viewing binary files of the class is extremely reliable and accurate, that is, I have recognized the tool tampering.

V. compiler comparison and symptoms Section

Now you may wish to download the JDK
The default minor. Major version of the class compiled by the 1.1 to JDK 1.7 compiler. (Go to Sun again
I have never used antiques on my website)

JDK compiler version Target
Parameters
Hexadecimal minor. Major Minor. Major in decimal format
Jdk1.1.8 No
Target Parameter
00 03 00 2D 45.3
Jdk1.2.2 No
Band (-target 1.1 by default)
00 03 00 2D 45.3
Jdk1.2.2 -Target
1.2
00 00 00 2e 46.0
Jdk1.3.1 _ 19 No
Band (-target 1.1 by default)
00 03 00 2D 45.3
Jdk1.3.1 _ 19 -Target
1.3
00 00 00 2f 47.0
J2sdk1.4.2 _ 10 No
Band (-Target 1.2 by default)
00 00 00 2e 46.0
J2sdk1.4.2 _ 10 -Target
1.4
00 00 30 48.0
Jdk1.5.0 _ 11 No
Band (-target 1.5 by default)
00 00 00 31 49.0
Jdk1.5.0 _ 11 -Target
1.4-source 1.4
00 00 30 48.0
Jdk1.6.0 _ 01 No
Band (-target 1.6 by default)
00 00 00 32 50.0
Jdk1.6.0 _ 01 -Target
1.5
00 00 00 31 49.0
Jdk1.6.0 _ 01 -Target
1.4-source 1.4
00 00 30 48.0
Jdk1.7.0 No
Band (-target 1.6 by default)
00 00 00 32 50.0
Jdk1.7.0 -Target
1.7
00 00 00 33 51.0
Jdk1.7.0 -Target
1.4-source 1.4
00 00 30 48.0
Apache
Harmony 5.0m3
No (default value:-Target 1.2) 00 00 00 2e 46.0
Apache
Harmony 5.0m3
-Target 1.4 00 00 30 48.0

Upper
This is a comparison of the JDK compiler on the Windows platform. We can summarize the following:

1)-target 1.1
The version number is one time. Target is 1.2 and later, only the primary version is used. The minor version number is 0.
2) the differences between 1.1 and 1.4 are small, so 1.2
1.4 none of the default targets are their own corresponding versions
3) the syntax of 1.5 is greatly changed, so the default target is 1.5 by default. This is also because
JDK 1.5 must generate code with the target of 1.4. If-target 1.4 is not enough,-source must be included at the same time.
1.4. Specify the source code compatibility. This is also true for the 1.6/1.7 JDK generation target code 1.4.
4) The 1.6 compiler seems radical, and the default parameter is
-Target 1.6. Because there is no difference between the syntax of 1.6 and 1.5, you do not need to follow-source 1.5 when using-target 1.5.
5)
Note that the default target for 1.7 compilation is 1.6.
6) The format version of the class file generated by other third-party JDK is the same as that of the corresponding sun version.
JDK
7) Most importantly, a version of JVM can accept Class
The maximum major version number of the file cannot exceed the version number of the class file compiled by the corresponding JDK with the corresponding target parameter
.

The above sentence
A little long. Reading the past in one breath is not easy to understand. For example, 1.4 of JVM can accept that the main version number of the maximum class file cannot exceed 1.4 JDK with parameters.
-The main version number of the class file compiled at target 1.4, that is, 48.

Because the default target is 1.5 when JDK is compiled
1.5, the byte code Major. Minor version is 49.0, so 1.4 of JVM is unacceptable, only throw an error.

That
Why JDK from 1.1 to 1.2, from 1.2 to 1.3, or from 1.3 to 1.4?
The Unsupported Major. Minor version error will not occur during the upgrade. That is because 1.2/1.3/1.4
Both of them maintain good binary compatibility.
See if the default target of 1.2/1.3/1.4 is 1.1/1.1/1.2, respectively.
That is, by default, the class files compiled by JDK 1.4 can be loaded and executed under JVM 1.2, not to mention JVM 1.3.
What about it? (Of course, the new version of the extended API must be removed)

VI:
Find the solution to the problem



Now, if you encounter such a problem, you should know how to solve it, and find
1.4 JDK download and install, and then use it to re-compile all the code? In fact, you don't have to worry about it. We must remember that javac has a-Target
The parameter is correct. You can continue to use JDK 1.5. during compilation, the parameter-target 1.4-source 1.4 will be included. However, you must
The API is well known when JDK 1.5 is added. The method not found cannot be obtained from your class file under JVM 1.4. Target
If the JVM is 1.3,-target 1.3-source 1.3 will be used for compilation.

If ant is used
You can also select target and source for a javac task.

<Javactarget = "1.4"
Source = "1.4".../>

If it is under development, it is certain that
Java ide also has compilation options for projects to set the target code. For example, Java compiler settings in Eclipse project properties,

Set the compilation options by yourself.
Compiler compliance level is, generated class files compatibility and Source
Compatibility
You can also adjust the two items manually. After manual configuration, you don't need to care about the version of the compiler you are using. You just need to ask him to generate the bytecode we want, again, even if the source code is
As long as the bytecode that can be compiled into JVM can be executed, it is not hard. You can also find the corresponding Setting Dialog Box in other ides.

At other times, you must know
What is the current JVM version and what is the major version number of the bytecode acceptable (can be compared with the previous table ). There are two ways to obtain information about the current JVM version:

First, if you are straight
Use Java commands to execute programs on the console. You can use Java-version to view the current JVM version, and then determine the acceptable class file version.

The
2. If it is executed in the container, it is not clear which one will be used
JVM, you can add the code system. getproperty ("Java. runtime. Version"); or
System. getproperty ("Java. Class. Version") to obtain the JVM version and acceptable class version.

Most
Next trick: if you do not want to recompile all the code with the target parameter for the lower version of JVM, if you still want to continue using the new API in the code
-Target 1.5-source 1.4
The Code cannot be compiled, and you have to refresh the code. The last trick is that you don't need to start with the source code, directly convert the bytecode that you compiled normally, and continue to enjoy those new features and new features.
API, that is: refer to the previous log: retrotranslator
Allows you to run the code written with the jdk1.5 feature in jvm1.4
That's how I use it. If you do a good test, there will be no problems.

7. discuss another problem




This
It is an unsupported Major. Minor version 49.0 error caused by copying tomcat. Scenario: My local installation is
JDK 1.5, and then find an EXE tomcat installation file on the Internet to install and be available. Later, my colleague asked for
Tomcat, do not want to download or install, so according to my previous experience, I copied the entire Tomcat directory to him. The result is that he browsed the JSP files and showed up.
Unsupported Major. Minor version 49.0 error. It can be confirmed that the version he installed is 1.4
JDK, but I still have some questions. I was dumb with confidence in this issue. Inertial thinking is to compile a class file to get a lower version of JVM.
If an exception occurs, it is not necessary to execute classes compiled by JDK 1.5.

Later, I read the exception information carefully and finally found out
% Atat_home %/common/lib/tools. jar indicates that the JSP file depends on it for compilation.
For a look at a class file in, 49.0, and soon I realized that this file was originally installed on my machine by the Tomcat installer from
% Jdk1.5 %/lib directory copied to Tomcat's lib directory, resulting in a 1.4 JVM with 49.0
The tools. jar of JDK 1.4 can be used to replace tomcat.

8. Summary




Its
The actual understanding of major. Minor is as much as we can imagine. It is also a Microsoft program, and a 32-bit application cannot be executed in a 16-bit system.

For example
If we know the target JVM version before release, how can we see major. Minor from the Java class file?
If the version is used, you do not have to wait until the server reports an exception to solve the problem.

In other cases, we should solve this problem in detail. In short, the root cause of the problem is the lower version.
.

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.