• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to check the java compiler version from a java class file?

September 12, 2013 //  by Krishna Srinivasan//  Leave a Comment

This tutorial provides a great way to find Java compiler version for a class file generated. It is one of the common problem among Java developers to know the exact version in which the JAR file is created. If you receive a JAR file, can you find what is the target compiler version for that JAR file to be deployed?. If developer not aware of the details, it is difficult for them to test the compatibility issues. If the run the code with different version, runtime exception will be thrown. Before start reading this article, If you want to understand the basics of Java compiler and JVM architecture, please go through the articles Java Compiler API, JVM, JRE and JDK difference and JVM.

java.lang.UnsupportedClassVersionError: test class :
 Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)

Without knowing the Java version in which the JAR is created is difficult for the developers. There is no way to check the version in the JAR file itself. We can check the version details only using the class files inside the JAR file. Unzip the JAR and take out any one class file to be verified.

  • JDK 1.0 — major version 45 and minor version 3
  • DK 1.1 — major version 45 and minor version 3
  • JDK 1.2 — major version 46 and minor version 0
  • JDK 1.3 — major version 47 and minor version 0
  • JDK 1.4 — major version 48 and minor version 0
  • JDK 1.5 — major version 49 and minor version 0
  • JDK 1.6 — major version 50 and minor version 0
  • JDK 1.7 — major version 51 and minor version 0

These version could be different for each implementation of the compiler. The above details are specific to Oracle/Sun JDK.  JDK has javap command to find the version details.

javap -verbose

You will find the following details displayed on the screen.

javapjavapThere is simple program which can get you the Java version details. Run the below program by passing the class file name as the arguments, this would return you the major version details.

package com;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class GetVersion {
public static void main(String[] args) throws IOException {
    System.out.println(getVersionDetails(args[0]));
}
public static String getVersionDetails(String filename)
        throws IOException {
     String version = "";
     DataInputStream stream = new DataInputStream(new FileInputStream(filename));
     int magicBytes = stream.readInt();
     if (magicBytes != 0xcafebabe) {
        System.out.println(filename + " is not a valid java file!");
     } else {
        int minorVersion = stream.readUnsignedShort();
        int majorVersion = stream.readUnsignedShort();
        version = majorVersion + "." + minorVersion;
     }
     stream.close();
     return version;
  }
}

javap command returns the complete details of the bytecode. In that there is major version and minor version which is for identifying the Java version details. I hope this tutorial have provided nice tip on finding the version details for the Java class. If you have any more tips, please contact us, we will publish it in our website.

Reference Books:

  • Java: The Complete Reference
  • Head First Java by Kathy Sierra, Bert Bates

Category: Java

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « How To Create Bar Chart Using DOJO?
Next Post: How To Use DOJO Object Store? »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact