1 /*
   2  * Copyright (c) 1999, 2016, 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 java.lang;
  27 
  28 import java.io.PrintStream;
  29 
  30 class Version {
  31 
  32 
  33     private static final String launcher_name =
  34         "@@LAUNCHER_NAME@@";
  35 
  36     private static final String java_version =
  37         "@@VERSION_SHORT@@";
  38 
  39     private static final String java_runtime_name =
  40         "@@RUNTIME_NAME@@";
  41 
  42     private static final String java_runtime_version =
  43         "@@VERSION_STRING@@";
  44 
  45     static {
  46         init();
  47     }
  48 
  49     public static void init() {
  50         System.setProperty("java.version", java_version);
  51         System.setProperty("java.runtime.version", java_runtime_version);
  52         System.setProperty("java.runtime.name", java_runtime_name);
  53     }
  54 
  55     private static boolean versionsInitialized = false;
  56     private static int jvm_major_version = 0;
  57     private static int jvm_minor_version = 0;
  58     private static int jvm_security_version = 0;
  59     private static int jvm_patch_version = 0;
  60     private static int jvm_build_number = 0;
  61     private static int jdk_major_version = 0;
  62     private static int jdk_minor_version = 0;
  63     private static int jdk_security_version = 0;
  64     private static int jdk_patch_version = 0;
  65     private static int jdk_build_number = 0;
  66 
  67     /**
  68      * In case you were wondering this method is called by java -version.
  69      * Sad that it prints to stderr; would be nicer if default printed on
  70      * stdout.
  71      */
  72     public static void print() {
  73         print(System.err);
  74     }
  75 
  76     /**
  77      * This is the same as print except that it adds an extra line-feed
  78      * at the end, typically used by the -showversion in the launcher
  79      */
  80     public static void println() {
  81         print(System.err);
  82         System.err.println();
  83     }
  84 
  85     /**
  86      * Give a stream, it will print version info on it.
  87      */
  88     public static void print(PrintStream ps) {
  89         boolean isHeadless = false;
  90 
  91         /* Report that we're running headless if the property is true */
  92         String headless = System.getProperty("java.awt.headless");
  93         if ( (headless != null) && (headless.equalsIgnoreCase("true")) ) {
  94             isHeadless = true;
  95         }
  96 
  97         /* First line: platform version. */
  98         ps.println(launcher_name + " version \"" + java_version + "\"");
  99 
 100         /* Second line: runtime version (ie, libraries). */
 101 
 102         String jdk_debug_level = System.getProperty("jdk.debug", "release");
 103         /* Debug level is not printed for "release" builds */
 104         if ("release".equals(jdk_debug_level)) {
 105             jdk_debug_level = "";
 106         } else {
 107             jdk_debug_level = jdk_debug_level + " ";
 108         }
 109 
 110         ps.print(java_runtime_name + " (" + jdk_debug_level + "build " + java_runtime_version);
 111 
 112         if (java_runtime_name.indexOf("Embedded") != -1 && isHeadless) {
 113             // embedded builds report headless state
 114             ps.print(", headless");
 115         }
 116         ps.println(')');
 117 
 118         /* Third line: JVM information. */
 119         String java_vm_name    = System.getProperty("java.vm.name");
 120         String java_vm_version = System.getProperty("java.vm.version");
 121         String java_vm_info    = System.getProperty("java.vm.info");
 122         ps.println(java_vm_name + " (" + jdk_debug_level + "build " + java_vm_version + ", " +
 123                    java_vm_info + ")");
 124     }
 125 
 126 
 127     /**
 128      * Returns the major version of the running JVM.
 129      * @return the major version of the running JVM
 130      * @since 1.6
 131      */
 132     public static synchronized int jvmMajorVersion() {
 133         if (!versionsInitialized) {
 134             initVersions();
 135         }
 136         return jvm_major_version;
 137     }
 138 
 139     /**
 140      * Returns the minor version of the running JVM.
 141      * @return the minor version of the running JVM
 142      * @since 1.6
 143      */
 144     public static synchronized int jvmMinorVersion() {
 145         if (!versionsInitialized) {
 146             initVersions();
 147         }
 148         return jvm_minor_version;
 149     }
 150 
 151 
 152     /**
 153      * Returns the security version of the running JVM.
 154      * @return the security version of the running JVM
 155      * @since 9
 156      */
 157     public static synchronized int jvmSecurityVersion() {
 158         if (!versionsInitialized) {
 159             initVersions();
 160         }
 161         return jvm_security_version;
 162     }
 163 
 164     /**
 165      * Returns the patch release version of the running JVM.
 166      * @return the patch release version of the running JVM
 167      * @since 9
 168      */
 169     public static synchronized int jvmPatchVersion() {
 170         if (!versionsInitialized) {
 171             initVersions();
 172         }
 173         return jvm_patch_version;
 174     }
 175 
 176     /**
 177      * Returns the build number of the running JVM.
 178      * @return the build number of the running JVM
 179      * @since 1.6
 180      */
 181     public static synchronized int jvmBuildNumber() {
 182         if (!versionsInitialized) {
 183             initVersions();
 184         }
 185         return jvm_build_number;
 186     }
 187 
 188     /**
 189      * Returns the major version of the running JDK.
 190      * @return the major version of the running JDK
 191      * @since 1.6
 192      */
 193     public static synchronized int jdkMajorVersion() {
 194         if (!versionsInitialized) {
 195             initVersions();
 196         }
 197         return jdk_major_version;
 198     }
 199 
 200     /**
 201      * Returns the minor version of the running JDK.
 202      * @return the minor version of the running JDK
 203      * @since 1.6
 204      */
 205     public static synchronized int jdkMinorVersion() {
 206         if (!versionsInitialized) {
 207             initVersions();
 208         }
 209         return jdk_minor_version;
 210     }
 211 
 212     /**
 213      * Returns the security version of the running JDK.
 214      * @return the security version of the running JDK
 215      * @since 9
 216      */
 217     public static synchronized int jdkSecurityVersion() {
 218         if (!versionsInitialized) {
 219             initVersions();
 220         }
 221         return jdk_security_version;
 222     }
 223 
 224     /**
 225      * Returns the patch release version of the running JDK.
 226      * @return the patch release version of the running JDK
 227      * @since 9
 228      */
 229     public static synchronized int jdkPatchVersion() {
 230         if (!versionsInitialized) {
 231             initVersions();
 232         }
 233         return jdk_patch_version;
 234     }
 235 
 236     /**
 237      * Returns the build number of the running JDK.
 238      * @return the build number of the running JDK
 239      * @since 1.6
 240      */
 241     public static synchronized int jdkBuildNumber() {
 242         if (!versionsInitialized) {
 243             initVersions();
 244         }
 245         return jdk_build_number;
 246     }
 247 
 248     private static synchronized void initVersions() {
 249         if (versionsInitialized) {
 250             return;
 251         }
 252         if (!getJvmVersionInfo()) {
 253             throw new InternalError("Unable to obtain JVM version info");
 254         }
 255         getJdkVersionInfo();
 256         versionsInitialized = true;
 257     }
 258 
 259     // Gets the JVM version info if available and sets the jvm_*_version fields
 260     // and its capabilities.
 261     private static native boolean getJvmVersionInfo();
 262     private static native void getJdkVersionInfo();
 263 }
 264 
 265 // Help Emacs a little because this file doesn't end in .java.
 266 //
 267 // Local Variables: ***
 268 // mode: java ***
 269 // End: ***