View the class file version at will

Source: Internet
Author: User

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:

  1. Package com. syj;
  2. Import java. Io. file;
  3. Import java. Io. fileinputstream;
  4. Import java. Io. ioexception;
  5. /**
  6. * <P>
  7. * Title: class file parser
  8. * </P>
  9. *
  10. * <P>
  11. * Copyright: Reprinted please indicate the source http://blog.csdn.net/sunyujia/
  12. * </P>
  13. *
  14. * @ Author Sun Xiaojia
  15. * @ Main sunyujia@yahoo.cn
  16. * @ Date DEC 4, 2008 9:27:10
  17. */
  18. Public class classfileparser {
  19. /**
  20. *
  21. * Description: main method for parsing class files
  22. *
  23. * @ Param File
  24. * @ Return
  25. * @ Throws ioexception
  26. * @ Mail sunyujia@yahoo.cn
  27. * @ Since: December 4, 2008 9:31:41
  28. */
  29. Public static classinfo parse (File file) throws ioexception {
  30. Fileinputstream FCM = NULL;
  31. Byte [] rs = NULL;
  32. Try {
  33. FS = new fileinputstream (File );
  34. Rs = new byte [8];
  35. FS. Read (RS );
  36. } Finally {
  37. If (FS! = NULL)
  38. FCM. Close ();
  39. }
  40. // System. Out. println ("class file first 8 Bytes:" + byte2hexstring (RS ));
  41. Classinfo = new classinfo ();
  42. Classinfo. setmajorversion (integer. parseint (
  43. Byte2hexstring (New byte [] {Rs [7]}), 16 ));
  44. Classinfo. setminorversion (integer. parseint (
  45. Byte2hexstring (New byte [] {Rs [5]}), 16 ));
  46. Classinfo. setfilepath (file. getabsolutepath ());
  47. Classinfo. setdevversion (getdevversion (classinfo. getmajorversion ()));
  48. Return classinfo;
  49. }
  50. /**
  51. *
  52. * Description: Convert byte array to hexadecimal
  53. *
  54. * @ Param B
  55. * @ Return
  56. * @ Mail sunyujia@yahoo.cn
  57. * @ Since: December 4, 2008 10:47:31
  58. */
  59. Public static string byte2hexstring (byte [] B ){
  60. Stringbuffer sb = new stringbuffer ();
  61. For (INT I = 0; I <B. length; I ++ ){
  62. String hex = integer. tohexstring (B [I] & 0xff );
  63. If (Hex. Length () = 1 ){
  64. Hex = '0' + hex;
  65. }
  66. SB. append (Hex. touppercase ());
  67. }
  68. Return sb. tostring ();
  69. }
  70. /**
  71. *
  72. * Description: Calculate the development master version number based on the major main version number.
  73. *
  74. * @ Param m
  75. * @ Return
  76. * @ Mail sunyujia@yahoo.cn
  77. * @ Since: December 4, 2008 10:48:29
  78. */
  79. Public static string getdevversion (INT m ){
  80. Int major = 45;
  81. Int devversion = 1;
  82. Return string. valueof (devversion + (m-Major + 1)/10f ));
  83. }
  84. }

  1. Package com. syj;
  2. Public class classinfo {
  3. /**
  4. * Main version number
  5. */
  6. Private int majorversion;
  7. /**
  8. * Minor version number
  9. */
  10. Private int minorversion;
  11. /**
  12. * Class file path
  13. */
  14. Private string filepath;
  15. /**
  16. * Development version
  17. */
  18. Private string devversion;
  19. Public int getmajorversion (){
  20. Return majorversion;
  21. }
  22. Public void setmajorversion (INT majorversion ){
  23. This. majorversion = majorversion;
  24. }
  25. Public int getminorversion (){
  26. Return minorversion;
  27. }
  28. Public void setminorversion (INT minorversion ){
  29. This. minorversion = minorversion;
  30. }
  31. Public String getfilepath (){
  32. Return filepath;
  33. }
  34. Public void setfilepath (string filepath ){
  35. This. filepath = filepath;
  36. }
  37. Public String tostring (){
  38. Return "Major. Minor:" + majorversion + "." + minorversion + "development version :"
  39. + Devversion;
  40. }
  41. Public String getdevversion (){
  42. Return devversion;
  43. }
  44. Public void setdevversion (string devversion ){
  45. This. devversion = devversion;
  46. }
  47. }
  1. Package com. syj;
  2. Import java. Io. file;
  3. /**
  4. * <P>
  5. * Title: program entry for viewing the class file version
  6. * </P>
  7. * <P>
  8. * Copyright: Reprinted please indicate the source http://blog.csdn.net/sunyujia/
  9. * </P>
  10. *
  11. * @ Author Sun Xiaojia
  12. * @ Main sunyujia@yahoo.cn
  13. * @ Date DEC 4, 2008 9:26:39
  14. */
  15. Public class runner {
  16. Public static void main (string [] ARGs) throws exception {
  17. If (ARGs = NULL | args. Length = 0 ){
  18. System. Out. println ("no file specified ");
  19. } Else {
  20. Classinfo = classfileparser. parse (new file (ARGs [0]);
  21. System. Out. println (classinfo );
  22. }
  23. }
  24. }

Source code for initiating program batch processing:

  1. @ Echo off
  2. Mode con: Cols = 110 lines = 15
  3. : Index
  4. Color 27
  5. CLS
  6. Echo.
  7. Echo welcome to the class file version gadgets
  8. Echo my blog: http://blog.csdn.net/sunyujia/
  9. 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.
  10. Echo.
  11. Echo.
  12. Set route = % Cd %
  13. Set Ravel =
  14. Set/P Ravel = drag the class file to be viewed:
  15. Set "Ravel = % Ravel:" = %"
  16. If/I "% Ravel :~ -6% "=". Class "If exist" % Ravel % "Goto go
  17. CLS
  18. ECHO file error. The specified file does not exist or is not a class file!
  19. Echo press any key to re-enter...
  20. Echo.
  21. Echo.
  22. Echo press any key to re-enter...
  23. Pause> NUL
  24. Goto Index
  25. : Go
  26. Java-jar versionviewer. Jar % Ravel %
  27. Echo.
  28. Echo press any key to exit...
  29. Pause> NUL
  30. Exit

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.