Reprinted please indicate the source: http://blog.csdn.net/sunyujia/
I have been busy working recently. I have been playing PSP after work and have not taken care of my blog for a long time. I wanted to check the compiled version of a class file the day before. I felt very troublesome. I need to check the hexadecimal format, so I wrote a small tool last night, just a few lines of code, and there was no research value. I posted it mainly for your convenience. in the future, you don't have to look at hexadecimal.
See http://blog.csdn.net/sunyujia/archive/2008/01/06/2027399.aspx for all version numbers
This program
Http://download.csdn.net/source/841562
Usage illustration:
The source code is as follows:
- Package com. syj;
- Import java. Io. file;
- Import java. Io. fileinputstream;
- Import java. Io. ioexception;
- /**
- * <P>
- * Title: class file parser
- * </P>
- *
- * <P>
- * Copyright: Reprinted please indicate the source http://blog.csdn.net/sunyujia/
- * </P>
- *
- * @ Author Sun Xiaojia
- * @ Main sunyujia@yahoo.cn
- * @ Date DEC 4, 2008 9:27:10
- */
- Public class classfileparser {
- /**
- *
- * Description: main method for parsing class files
- *
- * @ Param File
- * @ Return
- * @ Throws ioexception
- * @ Mail sunyujia@yahoo.cn
- * @ Since: December 4, 2008 9:31:41
- */
- Public static classinfo parse (File file) throws ioexception {
- Fileinputstream FCM = NULL;
- Byte [] rs = NULL;
- Try {
- FS = new fileinputstream (File );
- Rs = new byte [8];
- FS. Read (RS );
- } Finally {
- If (FS! = NULL)
- FCM. Close ();
- }
- // System. Out. println ("class file first 8 Bytes:" + byte2hexstring (RS ));
- Classinfo = new classinfo ();
- Classinfo. setmajorversion (integer. parseint (
- Byte2hexstring (New byte [] {Rs [7]}), 16 ));
- Classinfo. setminorversion (integer. parseint (
- Byte2hexstring (New byte [] {Rs [5]}), 16 ));
- Classinfo. setfilepath (file. getabsolutepath ());
- Classinfo. setdevversion (getdevversion (classinfo. getmajorversion ()));
- Return classinfo;
- }
- /**
- *
- * Description: Convert byte array to hexadecimal
- *
- * @ Param B
- * @ Return
- * @ Mail sunyujia@yahoo.cn
- * @ Since: December 4, 2008 10:47:31
- */
- Public static string byte2hexstring (byte [] B ){
- Stringbuffer sb = new stringbuffer ();
- For (INT I = 0; I <B. length; I ++ ){
- String hex = integer. tohexstring (B [I] & 0xff );
- If (Hex. Length () = 1 ){
- Hex = '0' + hex;
- }
- SB. append (Hex. touppercase ());
- }
- Return sb. tostring ();
- }
- /**
- *
- * Description: Calculate the development master version number based on the major main version number.
- *
- * @ Param m
- * @ Return
- * @ Mail sunyujia@yahoo.cn
- * @ Since: December 4, 2008 10:48:29
- */
- Public static string getdevversion (INT m ){
- Int major = 45;
- Int devversion = 1;
- Return string. valueof (devversion + (m-Major + 1)/10f ));
- }
- }
- Package com. syj;
- Public class classinfo {
- /**
- * Main version number
- */
- Private int majorversion;
- /**
- * Minor version number
- */
- Private int minorversion;
- /**
- * Class file path
- */
- Private string filepath;
- /**
- * Development version
- */
- Private string devversion;
- Public int getmajorversion (){
- Return majorversion;
- }
- Public void setmajorversion (INT majorversion ){
- This. majorversion = majorversion;
- }
- Public int getminorversion (){
- Return minorversion;
- }
- Public void setminorversion (INT minorversion ){
- This. minorversion = minorversion;
- }
- Public String getfilepath (){
- Return filepath;
- }
- Public void setfilepath (string filepath ){
- This. filepath = filepath;
- }
- Public String tostring (){
- Return "Major. Minor:" + majorversion + "." + minorversion + "development version :"
- + Devversion;
- }
- Public String getdevversion (){
- Return devversion;
- }
- Public void setdevversion (string devversion ){
- This. devversion = devversion;
- }
- }
- Package com. syj;
- Import java. Io. file;
- /**
- * <P>
- * Title: program entry for viewing the class file version
- * </P>
- * <P>
- * Copyright: Reprinted please indicate the source http://blog.csdn.net/sunyujia/
- * </P>
- *
- * @ Author Sun Xiaojia
- * @ Main sunyujia@yahoo.cn
- * @ Date DEC 4, 2008 9:26:39
- */
- Public class runner {
- Public static void main (string [] ARGs) throws exception {
- If (ARGs = NULL | args. Length = 0 ){
- System. Out. println ("no file specified ");
- } Else {
- Classinfo = classfileparser. parse (new file (ARGs [0]);
- System. Out. println (classinfo );
- }
- }
- }
Source code for initiating program batch processing:
- @ Echo off
- Mode con: Cols = 110 lines = 15
- : Index
- Color 27
- CLS
- Echo.
- Echo welcome to the class file version gadgets
- Echo my blog: http://blog.csdn.net/sunyujia/
- Echo drag the class file to this window, click the window, and press enter to display the JDK version of the compiled target class file.
- Echo.
- Echo.
- Set route = % Cd %
- Set Ravel =
- Set/P Ravel = drag the class file to be viewed:
- Set "Ravel = % Ravel:" = %"
- If/I "% Ravel :~ -6% "=". Class "If exist" % Ravel % "Goto go
- CLS
- ECHO file error. The specified file does not exist or is not a class file!
- Echo press any key to re-enter...
- Echo.
- Echo.
- Echo press any key to re-enter...
- Pause> NUL
- Goto Index
- : Go
- Java-jar versionviewer. Jar % Ravel %
- Echo.
- Echo press any key to exit...
- Pause> NUL
- Exit