1 /*
   2  * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.misc;
  27 import java.io.PrintStream;
  28 
  29 public class Version {
  30 
  31 
  32     private static final String launcher_name =
  33         "@@LAUNCHER_NAME@@";
  34 
  35     private static final String java_version =
  36         "@@VERSION_SHORT@@";
  37 
  38     private static final String java_runtime_name =
  39         "@@RUNTIME_NAME@@";
  40 
  41     private static final String java_runtime_version =
  42         "@@VERSION_STRING@@";
  43 
  44     static {
  45         init();
  46     }
  47 
  48     public static void init() {
  49         System.setProperty("java.version", java_version);
  50         System.setProperty("java.runtime.version", java_runtime_version);
  51         System.setProperty("java.runtime.name", java_runtime_name);
  52     }
  53 
  54     private static boolean versionsInitialized = false;
  55     private static int jvm_major_version = 0;
  56     private static int jvm_minor_version = 0;
  57     private static int jvm_security_version = 0;
  58     private static int jvm_patch_version = 0;
  59     private static int jvm_build_number = 0;
  60     private static int jdk_major_version = 0;
  61     private static int jdk_minor_version = 0;
  62     private static int jdk_security_version = 0;
  63     private static int jdk_patch_version = 0;
  64     private static int jdk_build_number = 0;
  65 
  66     /**
  67      * In case you were wondering this method is called by java -version.
  68      * Sad that it prints to stderr; would be nicer if default printed on
  69      * stdout.
  70      */
  71     public static void print() {
  72         print(System.err);
  73     }
  74 
  75     /**
  76      * This is the same as print except that it adds an extra line-feed
  77      * at the end, typically used by the -showversion in the launcher
  78      */
  79     public static void println() {
  80         print(System.err);
  81         System.err.println();
  82     }
  83 
  84     /**
  85      * Give a stream, it will print version info on it.
  86      */
  87     public static void print(PrintStream ps) {
  88         boolean isHeadless = false;
  89 
  90         /* Report that we're running headless if the property is true */
  91         String headless = System.getProperty("java.awt.headless");
  92         if ( (headless != null) && (headless.equalsIgnoreCase("true")) ) {
  93             isHeadless = true;
  94         }
  95 
  96         /* First line: platform version. */
  97         ps.println(launcher_name + " version \"" + java_version + "\"");
  98 
  99         /* Second line: runtime version (ie, libraries). */
 100 
 101         ps.print(java_runtime_name + " (build " + java_runtime_version);
 102 
 103         if (java_runtime_name.indexOf("Embedded") != -1 && isHeadless) {
 104             // embedded builds report headless state
 105             ps.print(", headless");
 106         }
 107         ps.println(')');
 108 
 109         /* Third line: JVM information. */
 110         String java_vm_name    = System.getProperty("java.vm.name");
 111         String java_vm_version = System.getProperty("java.vm.version");
 112         String java_vm_info    = System.getProperty("java.vm.info");
 113         ps.println(java_vm_name + " (build " + java_vm_version + ", " +
 114                    java_vm_info + ")");
 115     }
 116 
 117 
 118     /**
 119      * Returns the major version of the running JVM if it's 1.6 or newer
 120      * or any RE VM build. It will return 0 if it's an internal 1.5 or
 121      * 1.4.x build.
 122      *
 123      * @since 1.6
 124      */
 125     public static synchronized int jvmMajorVersion() {
 126         if (!versionsInitialized) {
 127             initVersions();
 128         }
 129         return jvm_major_version;
 130     }
 131 
 132     /**
 133      * Returns the minor version of the running JVM if it's 1.6 or newer
 134      * or any RE VM build. It will return 0 if it's an internal 1.5 or
 135      * 1.4.x build.
 136      * @since 1.6
 137      */
 138     public static synchronized int jvmMinorVersion() {
 139         if (!versionsInitialized) {
 140             initVersions();
 141         }
 142         return jvm_minor_version;
 143     }
 144 
 145 
 146     /**
 147      * Returns the security version of the running JVM 
 148      * @since 1.6
 149      */
 150     public static synchronized int jvmSecurityVersion() {
 151         if (!versionsInitialized) {
 152             initVersions();
 153         }
 154         return jvm_security_version;
 155     }
 156 
 157     /**
 158      * Returns the patch release version of the running JVM
 159      * @since JDK9
 160      */
 161     public static synchronized int jvmPatchVersion() {
 162         if (!versionsInitialized) {
 163             initVersions();
 164         }
 165         return jvm_patch_version;
 166     }
 167 
 168     /**
 169      * Returns the build number of the running JVM
 170      * @since 1.6
 171      */
 172     public static synchronized int jvmBuildNumber() {
 173         if (!versionsInitialized) {
 174             initVersions();
 175         }
 176         return jvm_build_number;
 177     }
 178 
 179     /**
 180      * Returns the major version of the running JDK.
 181      *
 182      * @since 1.6
 183      */
 184     public static synchronized int jdkMajorVersion() {
 185         if (!versionsInitialized) {
 186             initVersions();
 187         }
 188         return jdk_major_version;
 189     }
 190 
 191     /**
 192      * Returns the minor version of the running JDK.
 193      * @since 1.6
 194      */
 195     public static synchronized int jdkMinorVersion() {
 196         if (!versionsInitialized) {
 197             initVersions();
 198         }
 199         return jdk_minor_version;
 200     }
 201 
 202     /**
 203      * Returns the security version of the running JDK.
 204      * @since 1.6
 205      */
 206     public static synchronized int jdkSecurityVersion() {
 207         if (!versionsInitialized) {
 208             initVersions();
 209         }
 210         return jdk_security_version;
 211     }
 212 
 213     /**
 214      * Returns the patch release version of the running JDK
 215      * @since JDK9
 216      */
 217     public static synchronized int jdkPatchVersion() {
 218         if (!versionsInitialized) {
 219             initVersions();
 220         }
 221         return jdk_patch_version;
 222     }
 223 
 224     /**
 225      * Returns the build number of the running JDK
 226      * @since 1.6
 227      */
 228     public static synchronized int jdkBuildNumber() {
 229         if (!versionsInitialized) {
 230             initVersions();
 231         }
 232         return jdk_build_number;
 233     }
 234 
 235     private static synchronized void initVersions() {
 236         if (versionsInitialized) {
 237             return;
 238         }
 239         if (!getJvmVersionInfo()) {
 240             throw new RuntimeException("Unable to obtain JVM version info");
 241         }
 242         getJdkVersionInfo();
 243         versionsInitialized = true;
 244     }
 245 
 246     // Gets the JVM version info if available and sets the jvm_*_version fields
 247     // and its capabilities.
 248     private static native boolean getJvmVersionInfo();
 249     private static native void getJdkVersionInfo();
 250 }
 251 
 252 // Help Emacs a little because this file doesn't end in .java.
 253 //
 254 // Local Variables: ***
 255 // mode: java ***
 256 // End: ***