getImplementationVersion-获取版本号

  在工作中会遇到这个方法的使用,就记录一下。

一:getImplementationVersion

1.  方法

  java.lang.Package.getImplementationVersion()方法实例。

  方法返回这个实现的版本。

  

2.返回值

  此方法返回执行的版本,如果尚不清楚,则返回null。

3.示例

 1 package start;
 2 
 3 public class TestVersion {
 4     public static void main(String[] args) {
 5         method1();
 6     }
 7     //method1
 8     public static void method1() {
 9         Package pack = Package.getPackage("java.lang");
10         System.out.println(pack.getImplementationVersion());
11     }
12 }

4.效果

  

二:工作中使用的地方

1.程序

1  public static String getVersion() {
2         String version = FrameworkInfo.class.getPackage().getImplementationVersion();
3         if (version == null || version.equals("")) {
4             version = defaultVersion;
5         }
6         return version;
7     }

2.实验类

 @Test
    public void testVersion(){
        Package packet= FrameworkInfo.class.getPackage();
        System.out.println(packet);
    }

3.效果

  

4.这个packet信息来源

  应该是打包出来的信息,可以看pom。

  

三:PS--getPacket

1.说明

  在上面的程序中使用到了这个方法,在这里了解一下。

2.程序

 1 package start;
 2 
 3 public class TestPacket {
 4     public static void main(String[] args) {
 5          Package[] pack = Package.getPackages();
 6          for (int i = 0; i < pack.length; i++) {
 7              System.out.println("" + pack[i]);
 8          }
 9     }
10 }

3.效果

  

  

原文地址:https://www.cnblogs.com/juncaoit/p/8204653.html