Check the Java class version number.

Source: Internet
Author: User

Patches are always played over and over again.

Sometimes, I am worried that someone will accidentally transfer the class of the higher version to the environment where the lower version JRE runs.

Easy to writeCodeCheck the version number of the class in the folder.

Package Org. wee. CV; import Java. io. file; import Java. io. fileinputstream; public class classversion {/*** check the version number of the class file * @ Param classfile * @ return *. The returned value is jdk1.4 jdk1.5... or unknown * @ throws exception */public static string checkclassversion (File classfile) throws exception {byte [] DATA = new byte [8]; fileinputstream in = new fileinputstream (classfile ); // read the first 8 bytes of the file. // The actual version number is written on 4-7 bytes (starting from 0th bytes) in. read (data, 0, 8); In. close (); // calculate the Primary and Secondary version of the class file. Int minor_version = (INT) data [4]) <8) + data [5]; int major_version = (INT) data [6]) <8) + data [7]; return translateversiontojdk (major_version);}/*** based on the main version number, convert to JDK version * 48 is jdk1.4, 49 is jdk1.5, and so on * @ Param major_version * @ return */public static string translateversiontojdk (final int major_version) {Switch (major_version) {Case 48: Return "jdk1.4"; Case 49: Return "jdk1.5"; Case 50: Return "jdk1.6"; Case 51: Return "jdk1.7"; default: return "unknown ";}}}

Package Org. wee. CV; import Java. io. file; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; public class batchclassversioncheck {public static void main (string [] ARGs) {try {batchclassversioncheck bcvc = new batchclassversioncheck (); hashmap <string, list <string> versionmap = bcvc. getdirectoryclassversioninfo (new file ("D:/test"); For (string version: versionmap. keyset () {system. out. println ("version:" + version); List <string> List = versionmap. get (version); For (string file: List) {system. out. println (File) ;}} catch (exception e) {e. printstacktrace () ;}/// Save the version information of the class file in the folder. // The key is the version number. // The value is the absolute path of the corresponding file. Private hashmap <string, list <string> classversioninfomap;/*** obtain the version information of the class in the folder * @ Param dir * @ return * @ throws exception */Public hashmap <string, list <string> getdirectoryclassversioninfo (File DIR) throws exception {classversioninfomap = new hashmap <string, list <string> (); searchclass (DIR); return classversioninfomap ;} /*** Recursive Method **: Search for the class file in the current folder, calculate the version information, and save it in map. * When a folder is found, recursive search * @ Param dir * @ throws exception */protected void searchclass (File DIR) throws exception {file [] childfiles = dir. listfiles (); For (File childfile: childfiles) {If (childfile. isdirectory () {// Recursively search the subfolders searchclass (childfile);} else {If (childfile. getname (). tolowercase (). endswith (". class ") {// search for the class file // record the version information in the putversioninfo (classversion. checkclassversion (childfile), childfile. getabsolutepath () ;}}}/*** record version information in map * @ Param version * @ Param absolutepath */private void putversioninfo (string version, string absolutepath) {list <string> List = NULL; If (classversioninfomap. containskey (Version) {list = classversioninfomap. get (Version) ;}else {list = new arraylist <string> () ;}list. add (absolutepath); classversioninfomap. put (version, list );}}

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.