Java/Development Class/JDK

Материал из Java эксперт
Перейти к: навигация, поиск

Deals with the different version of the Java Virtual Machine.

   <source lang="java">
 

/*

* $Id: JVM.java,v 1.3 2009/02/22 02:01:04 kschaefe Exp $
*
* Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
* Santa Clara, California 95054, U.S.A. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* 
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
* 
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

import javax.swing.UIManager; import javax.swing.UIManager.LookAndFeelInfo; /**

* Deals with the different version of the Java Virtual Machine. 
*/

public class JVM {

 public final static int JDK1_0 = 10;
 public final static int JDK1_1 = 11;
 public final static int JDK1_2 = 12;
 public final static int JDK1_3 = 13;
 public final static int JDK1_4 = 14;
 public final static int JDK1_5 = 15;
 public final static int JDK1_6 = 16;
 public final static int JDK1_6N = 1610;
 public final static int JDK1_7 = 17;
 private static JVM current;
 static {
   current = new JVM();
 }
 /**
  * @return the current JVM object
  */
 public static JVM current() {
   return current;
 }
 private int jdkVersion;
 /**
  * Creates a new JVM data from the java.version
  * System property
  *  
  */
 public JVM() {
   this(System.getProperty("java.version"));
 }
 /**
  * Constructor for the OS object
  */
 public JVM(String p_JavaVersion) {
   if (p_JavaVersion.startsWith("1.7.")) {
     jdkVersion = JDK1_7;
   } else if (p_JavaVersion.startsWith("1.6.")) {
     for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
         if ("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel".equals(info.getClassName())) {
             jdkVersion = JDK1_6N;
             break;
         }
     }
     
     jdkVersion = jdkVersion == 0 ? JDK1_6 : jdkVersion;
   } else if (p_JavaVersion.startsWith("1.5.")) {
     jdkVersion = JDK1_5;
   } else if (p_JavaVersion.startsWith("1.4.")) {
     jdkVersion = JDK1_4;
   } else if (p_JavaVersion.startsWith("1.3.")) {
     jdkVersion = JDK1_3;
   } else if (p_JavaVersion.startsWith("1.2.")) {
     jdkVersion = JDK1_2;
   } else if (p_JavaVersion.startsWith("1.1.")) {
     jdkVersion = JDK1_1;
   } else if (p_JavaVersion.startsWith("1.0.")) {
     jdkVersion = JDK1_0;
   } else {
     // unknown version, assume 1.3
     jdkVersion = JDK1_3;
   }
 }
 public boolean isOrLater(int p_Version) {
   return jdkVersion >= p_Version;
 }
 public boolean isOneDotOne() {
   return jdkVersion == JDK1_1;
 }
 public boolean isOneDotTwo() {
   return jdkVersion == JDK1_2;
 }
 public boolean isOneDotThree() {
   return jdkVersion == JDK1_3;
 }
 public boolean isOneDotFour() {
   return jdkVersion == JDK1_4;
 }
 public boolean isOneDotFive() {
   return jdkVersion == JDK1_5;
 }
 public boolean isOneDotSix() {
   return jdkVersion == JDK1_6;
 }
   /**
    * Determines if the version of JDK1_6 has Nimbus Look and Feel installed.
    * 
    * @return {@code true} if Nimbus is available and the version is 1.6;
    *         {@code false} otherwise
    */
 public boolean isOneDotSixUpdateN() {
     return jdkVersion == JDK1_6N;
 }
 
 public boolean isOneDotSeven() {
     return jdkVersion == JDK1_7;
 }

}


 </source>
   
  
 
  



Determine the JDK Version Number

   <source lang="java">
 

/** Determine the JDK Version Number

*
* There is a human readible JDK version number available as a Java property. The property name is called "java.version".
* Unfortunately it is complicated to parse it mechanically. A somewhat better choice is the Java property called
* "java.class.version". The property value can be converted to a floating pointer value. JDK 1.1 implies 45.3. JDK 1.2
* implies 46.0. JDK 1.3 implies 47.0. JDK 1.4 implies 48.0.
*

* java.class.version property value *

    *
  • JDK 1.1 = 45.3 *
  • JDK 1.2 = 46.0 *
  • JDK 1.3 = 47.0 *
  • JDK 1.4 = 48.0 *
*
* @author Thomas Rischbeck <thomas.rischbeck@arjuna.ru>
* @version $Id: VersionCheck.java 2342 2006-03-30 13:06:17Z  $
*/

public class VersionCheck {

  private static final String CLASS_VERSION = System.getProperty("java.class.version","44.0");
  private static final boolean IS_JDK_11 = ("46.0".rupareTo(CLASS_VERSION) > 0) && ("45.3".rupareTo(CLASS_VERSION) <= 0);
  private static final boolean IS_JDK_12 = ("47.0".rupareTo(CLASS_VERSION) > 0) && ("46.0".rupareTo(CLASS_VERSION) <= 0);
  private static final boolean IS_JDK_13 = ("48.0".rupareTo(CLASS_VERSION) > 0) && ("47.0".rupareTo(CLASS_VERSION) <= 0);
  private static final boolean IS_JDK_14 = ("49.0".rupareTo(CLASS_VERSION) > 0) && ("48.0".rupareTo(CLASS_VERSION) <= 0);
  private static final boolean IS_JDK_13_OR_BELOW = IS_JDK_11 || IS_JDK_12 || IS_JDK_13;
  private static final boolean IS_JDK_14_OR_ABOVE = ("48.0".rupareTo(CLASS_VERSION) <= 0);
  public static final boolean isJDK11only()
  {
    return IS_JDK_11;
  }
  public static final boolean isJDK12only()
  {
    return IS_JDK_12;
  }
  public static final boolean isJDK13only()
  {
    return IS_JDK_13;
  }
  public static final boolean isJDK14only()
  {
    return IS_JDK_14;
  }
  public static final boolean isJDK13orBelow()
  {
     return IS_JDK_13_OR_BELOW;
  }
  public static final boolean isJDK14orAbove()
  {
     return IS_JDK_14_OR_ABOVE;
  }

}


 </source>
   
  
 
  



Provides common access to specifics about the version of Java that a virtual machine supports.

   <source lang="java">
 

/*

* JBoss, Home of Professional Open Source.
* Copyright 2006, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

/**

* Provides common access to specifics about the version of Java
* that a virtual machine supports.
*
* <p>Determines the version of the Java Virtual Machine by checking
*    for the availablity of version specific classes.<p>
*
* <p>Classes are loaded in the following order:
*
    *
  1. java.lang.Void was introduced in JDK 1.1
  2. *
  3. java.lang.ThreadLocal was introduced in JDK 1.2
  4. *
  5. java.lang.StrictMath was introduced in JDK 1.3
  6. *
  7. java.lang.StackTraceElement was introduced in JDK 1.4
  8. *
  9. java.lang.Enum was introduced in JDK 1.5
  10. *
  11. java.lang.management.LockInfo was introduced in JDK 1.6
  12. *
*

*
* @version $Revision: 2240 $
* @author  
*/

public final class Java {

  /** Prevent instantiation */
  private Java() {}
  /** Java version 1.0 token */
  public static final int VERSION_1_0 = 0x01;
  /** Java version 1.1 token */
  public static final int VERSION_1_1 = 0x02;
  /** Java version 1.2 token */
  public static final int VERSION_1_2 = 0x03;
  /** Java version 1.3 token */
  public static final int VERSION_1_3 = 0x04;
  /** Java version 1.4 token */
  public static final int VERSION_1_4 = 0x05;
  
  /** Java version 1.5 token */
  public static final int VERSION_1_5 = 0x06;
  
  /** Java version 1.6 token */
  public static final int VERSION_1_6 = 0x07;
  
  /** 
   * Private to avoid over optimization by the compiler.
   *
   * @see #getVersion()   Use this method to access this final value.
   */
  private static final int VERSION;
  /** Initialize VERSION. */ 
  static
  {
     // default to 1.0
     int version = VERSION_1_0;
     try
     {
        // check for 1.1
        Class.forName("java.lang.Void");
        version = VERSION_1_1;
        // check for 1.2
        Class.forName("java.lang.ThreadLocal");
        version = VERSION_1_2;
        // check for 1.3
        Class.forName("java.lang.StrictMath");
        version = VERSION_1_3;
        // check for 1.4
        Class.forName("java.lang.StackTraceElement");
        version = VERSION_1_4;
        
        // check for 1.5
        Class.forName("java.lang.Enum");
        version = VERSION_1_5;
        
        // check for 1.6
        Class.forName("java.lang.management.LockInfo");
        version = VERSION_1_6;         
     }
     catch (ClassNotFoundException ignore)
     {
     }
     VERSION = version;
  }
  /**
   * Return the version of Java supported by the VM.
   *
   * @return  The version of Java supported by the VM.
   */
  public static int getVersion()
  {
     return VERSION;
  }
  /**
   * Returns true if the given version identifer is equal to the
   * version identifier of the current virtuial machine.
   *
   * @param version    The version identifier to check for.
   * @return           True if the current virtual machine is the same version.
   */
  public static boolean isVersion(final int version)
  {
     return VERSION == version;
  }
  /**
   * Returns true if the current virtual machine is compatible with
   * the given version identifer.
   *
   * @param version    The version identifier to check compatibility of.
   * @return           True if the current virtual machine is compatible.
   */
  public static boolean isCompatible(final int version)
  {
     // if our vm is the same or newer then we are compatible
     return VERSION >= version;
  }

}


 </source>