A Java class file is a file containing Java bytecode and having .class extension that can be executed by JVM. A Java class file is created by a Java compiler from .java files as a result of successful compilation. As we know that a single Java programming language source file (or we can say .java file) may contain one class or more than one class. So if a .java file has more than one class then each class will compile into a separate class files.
For Example: Save this below code as Test.java on your system.
For Compiling:
After compilation there will be 3 class files in corresponding folder named as:
A single class file structure contains attributes that describe a class file.
Representation of Class File Structure
Elements of class file are as follows:
// class Declaration class Sample < public static void main(String[] args) < System.out.println("Magic Number"); >>
Step 1: Compile using javac Sample.java
Step 2: Now open the Sample.class file. It will looks like following.
Step 3: Now erase at least single symbol from this Sample.class file from starting of file and save it.
Step 4: Now try to run this using java Sample command and see the magic i.e. you will get run time exception (See the highlighted text in below image):
Note: This can vary depending on how much you remove the .class file data.
Note: Lower version compiler generated .class file can be executed by high version JVM but higher version compiler generated .class file cannot be executed by lower version JVM. If we will try to execute we will get run time exception.
This demonstration is for Windows OS as follows:
Step 1: Open a command prompt window and try to check java compiler version and JVM version using following commands respectively (Highlighted text in image are the commands)
Output for 1.8 version will be:
Step 2: Now check with another version which may be higher or lower than already installed.thisDownload link.
And install this to your PC or laptops and note the installation address.
Step 3: Open a second command prompt window and set the path of bin folder of installed jdk installed during 2nd step. And check for Java compiler version ad JVM version.
Step 4: Now on 1st command prompt compile the any valid .java file. For example: See above Sample.java file. Compile it as: Compiling with Compiler Version 1.8" />
Step 5: Now on 2nd command prompt window try to run the above compiled code class file and see what happen. There is a run time exception which I have highlighted in below image.
Note: Internally jdk 1.5 version means 49.0 and 1.6 means 50.0 and 1.7 means 51.0 etc. class file version where the digits before the decimal point represent the major_version and digits after decimal point represents the minor_version.