1 /* 2 * Copyright (c) 1999, 2013, 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 "@@java_version@@"; 37 38 private static final String java_runtime_name = 39 "@@java_runtime_name@@"; 40 41 private static final String java_runtime_version = 42 "@@java_runtime_version@@"; 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_micro_version = 0; 58 private static int jvm_update_version = 0; 59 private static int jvm_build_number = 0; 60 private static String jvm_special_version = null; 61 private static int jdk_major_version = 0; 62 private static int jdk_minor_version = 0; 63 private static int jdk_micro_version = 0; 64 private static int jdk_update_version = 0; 65 private static int jdk_build_number = 0; 66 private static String jdk_special_version = null; 67 68 /** 69 * In case you were wondering this method is called by java -version. 70 * Sad that it prints to stderr; would be nicer if default printed on 71 * stdout. 72 */ 73 public static void print() { 74 print(System.err); 75 } 76 77 /** 78 * This is the same as print except that it adds an extra line-feed 79 * at the end, typically used by the -showversion in the launcher 80 */ 81 public static void println() { 82 print(System.err); 83 System.err.println(); 84 } 85 86 /** 87 * Give a stream, it will print version info on it. 88 */ 89 public static void print(PrintStream ps) { 90 boolean isHeadless = false; 91 92 /* Report that we're running headless if the property is true */ 93 String headless = System.getProperty("java.awt.headless"); 94 if ( (headless != null) && (headless.equalsIgnoreCase("true")) ) { 95 isHeadless = true; 96 } 97 98 /* First line: platform version. */ 99 ps.println(launcher_name + " version \"" + java_version + "\""); 100 101 /* Second line: runtime version (ie, libraries). */ 102 103 ps.print(java_runtime_name + " (build " + java_runtime_version); 104 105 if (java_runtime_name.indexOf("Embedded") != -1 && isHeadless) { 106 // embedded builds report headless state 107 ps.print(", headless"); 108 } 109 ps.println(')'); 110 111 /* Third line: JVM information. */ 112 String java_vm_name = System.getProperty("java.vm.name"); 113 String java_vm_version = System.getProperty("java.vm.version"); 114 String java_vm_info = System.getProperty("java.vm.info"); 115 ps.println(java_vm_name + " (build " + java_vm_version + ", " + 116 java_vm_info + ")"); 117 } 118 119 120 /** 121 * Returns the major version of the running JVM if it's 1.6 or newer 122 * or any RE VM build. It will return 0 if it's an internal 1.5 or 123 * 1.4.x build. 124 * 125 * @since 1.6 126 */ 127 public static synchronized int jvmMajorVersion() { 128 if (!versionsInitialized) { 129 initVersions(); 130 } 131 return jvm_major_version; 132 } 133 134 /** 135 * Returns the minor version of the running JVM if it's 1.6 or newer 136 * or any RE VM build. It will return 0 if it's an internal 1.5 or 137 * 1.4.x build. 138 * @since 1.6 139 */ 140 public static synchronized int jvmMinorVersion() { 141 if (!versionsInitialized) { 142 initVersions(); 143 } 144 return jvm_minor_version; 145 } 146 147 148 /** 149 * Returns the micro version of the running JVM if it's 1.6 or newer 150 * or any RE VM build. It will return 0 if it's an internal 1.5 or 151 * 1.4.x build. 152 * @since 1.6 153 */ 154 public static synchronized int jvmMicroVersion() { 155 if (!versionsInitialized) { 156 initVersions(); 157 } 158 return jvm_micro_version; 159 } 160 161 /** 162 * Returns the update release version of the running JVM if it's 163 * a RE build. It will return 0 if it's an internal build. 164 * @since 1.6 165 */ 166 public static synchronized int jvmUpdateVersion() { 167 if (!versionsInitialized) { 168 initVersions(); 169 } 170 return jvm_update_version; 171 } 172 173 public static synchronized String jvmSpecialVersion() { 174 if (!versionsInitialized) { 175 initVersions(); 176 } 177 if (jvm_special_version == null) { 178 jvm_special_version = getJvmSpecialVersion(); 179 } 180 return jvm_special_version; 181 } 182 public static native String getJvmSpecialVersion(); 183 184 /** 185 * Returns the build number of the running JVM if it's a RE build 186 * It will return 0 if it's an internal build. 187 * @since 1.6 188 */ 189 public static synchronized int jvmBuildNumber() { 190 if (!versionsInitialized) { 191 initVersions(); 192 } 193 return jvm_build_number; 194 } 195 196 /** 197 * Returns the major version of the running JDK. 198 * 199 * @since 1.6 200 */ 201 public static synchronized int jdkMajorVersion() { 202 if (!versionsInitialized) { 203 initVersions(); 204 } 205 return jdk_major_version; 206 } 207 208 /** 209 * Returns the minor version of the running JDK. 210 * @since 1.6 211 */ 212 public static synchronized int jdkMinorVersion() { 213 if (!versionsInitialized) { 214 initVersions(); 215 } 216 return jdk_minor_version; 217 } 218 219 /** 220 * Returns the micro version of the running JDK. 221 * @since 1.6 222 */ 223 public static synchronized int jdkMicroVersion() { 224 if (!versionsInitialized) { 225 initVersions(); 226 } 227 return jdk_micro_version; 228 } 229 230 /** 231 * Returns the update release version of the running JDK if it's 232 * a RE build. It will return 0 if it's an internal build. 233 * @since 1.6 234 */ 235 public static synchronized int jdkUpdateVersion() { 236 if (!versionsInitialized) { 237 initVersions(); 238 } 239 return jdk_update_version; 240 } 241 242 public static synchronized String jdkSpecialVersion() { 243 if (!versionsInitialized) { 244 initVersions(); 245 } 246 if (jdk_special_version == null) { 247 jdk_special_version = getJdkSpecialVersion(); 248 } 249 return jdk_special_version; 250 } 251 public static native String getJdkSpecialVersion(); 252 253 /** 254 * Returns the build number of the running JDK if it's a RE build 255 * It will return 0 if it's an internal build. 256 * @since 1.6 257 */ 258 public static synchronized int jdkBuildNumber() { 259 if (!versionsInitialized) { 260 initVersions(); 261 } 262 return jdk_build_number; 263 } 264 265 // true if JVM exports the version info including the capabilities 266 private static boolean jvmVersionInfoAvailable; 267 private static synchronized void initVersions() { 268 if (versionsInitialized) { 269 return; 270 } 271 jvmVersionInfoAvailable = getJvmVersionInfo(); 272 if (!jvmVersionInfoAvailable) { 273 // parse java.vm.version for older JVM before the 274 // new JVM_GetVersionInfo is added. 275 // valid format of the version string is: 276 // n.n.n[_uu[c]][-<identifer>]-bxx 277 CharSequence cs = System.getProperty("java.vm.version"); 278 if (cs.length() >= 5 && 279 Character.isDigit(cs.charAt(0)) && cs.charAt(1) == '.' && 280 Character.isDigit(cs.charAt(2)) && cs.charAt(3) == '.' && 281 Character.isDigit(cs.charAt(4))) { 282 jvm_major_version = Character.digit(cs.charAt(0), 10); 283 jvm_minor_version = Character.digit(cs.charAt(2), 10); 284 jvm_micro_version = Character.digit(cs.charAt(4), 10); 285 cs = cs.subSequence(5, cs.length()); 286 if (cs.charAt(0) == '_' && cs.length() >= 3 && 287 Character.isDigit(cs.charAt(1)) && 288 Character.isDigit(cs.charAt(2))) { 289 int nextChar = 3; 290 try { 291 String uu = cs.subSequence(1, 3).toString(); 292 jvm_update_version = Integer.valueOf(uu).intValue(); 293 if (cs.length() >= 4) { 294 char c = cs.charAt(3); 295 if (c >= 'a' && c <= 'z') { 296 jvm_special_version = Character.toString(c); 297 nextChar++; 298 } 299 } 300 } catch (NumberFormatException e) { 301 // not conforming to the naming convention 302 return; 303 } 304 cs = cs.subSequence(nextChar, cs.length()); 305 } 306 if (cs.charAt(0) == '-') { 307 // skip the first character 308 // valid format: <identifier>-bxx or bxx 309 // non-product VM will have -debug|-release appended 310 cs = cs.subSequence(1, cs.length()); 311 String[] res = cs.toString().split("-"); 312 for (String s : res) { 313 if (s.charAt(0) == 'b' && s.length() == 3 && 314 Character.isDigit(s.charAt(1)) && 315 Character.isDigit(s.charAt(2))) { 316 jvm_build_number = 317 Integer.valueOf(s.substring(1, 3)).intValue(); 318 break; 319 } 320 } 321 } 322 } 323 } 324 getJdkVersionInfo(); 325 versionsInitialized = true; 326 } 327 328 // Gets the JVM version info if available and sets the jvm_*_version fields 329 // and its capabilities. 330 // 331 // Return false if not available which implies an old VM (Tiger or before). 332 private static native boolean getJvmVersionInfo(); 333 private static native void getJdkVersionInfo(); 334 } 335 336 // Help Emacs a little because this file doesn't end in .java. 337 // 338 // Local Variables: *** 339 // mode: java *** 340 // End: ***