1 /* 2 * Copyright (c) 1999, 2012, 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_profile_name = 42 "@@java_profile_name@@"; 43 44 private static final String java_runtime_version = 45 "@@java_runtime_version@@"; 46 47 static { 48 init(); 49 } 50 51 public static void init() { 52 System.setProperty("java.version", java_version); 53 System.setProperty("java.runtime.version", java_runtime_version); 54 System.setProperty("java.runtime.name", java_runtime_name); 55 if (java_profile_name.length() > 0) 56 System.setProperty("java.runtime.profile", java_profile_name); 57 } 58 59 private static boolean versionsInitialized = false; 60 private static int jvm_major_version = 0; 61 private static int jvm_minor_version = 0; 62 private static int jvm_micro_version = 0; 63 private static int jvm_update_version = 0; 64 private static int jvm_build_number = 0; 65 private static String jvm_special_version = null; 66 private static int jdk_major_version = 0; 67 private static int jdk_minor_version = 0; 68 private static int jdk_micro_version = 0; 69 private static int jdk_update_version = 0; 70 private static int jdk_build_number = 0; 71 private static String jdk_special_version = null; 72 73 /** 74 * In case you were wondering this method is called by java -version. 75 * Sad that it prints to stderr; would be nicer if default printed on 76 * stdout. 77 */ 78 public static void print() { 79 print(System.err); 80 } 81 82 /** 83 * This is the same as print except that it adds an extra line-feed 84 * at the end, typically used by the -showversion in the launcher 85 */ 86 public static void println() { 87 print(System.err); 88 System.err.println(); 89 } 90 91 /** 92 * Give a stream, it will print version info on it. 93 */ 94 public static void print(PrintStream ps) { 95 boolean isHeadless = false; 96 97 /* Report that we're running headless if the property is true */ 98 String headless = System.getProperty("java.awt.headless"); 99 if ( (headless != null) && (headless.equalsIgnoreCase("true")) ) { 100 isHeadless = true; 101 } 102 103 /* First line: platform version. */ 104 ps.println(launcher_name + " version \"" + java_version + "\""); 105 106 /* Second line: runtime version (ie, libraries). */ 107 108 ps.print(java_runtime_name + " (build " + java_runtime_version); 109 110 if (java_profile_name.length() > 0) { 111 // profile name 112 ps.print(", profile " + java_profile_name); 113 } 114 115 if (java_runtime_name.indexOf("Embedded") != -1 && isHeadless) { 116 // embedded builds report headless state 117 ps.print(", headless"); 118 } 119 ps.println(')'); 120 121 /* Third line: JVM information. */ 122 String java_vm_name = System.getProperty("java.vm.name"); 123 String java_vm_version = System.getProperty("java.vm.version"); 124 String java_vm_info = System.getProperty("java.vm.info"); 125 ps.println(java_vm_name + " (build " + java_vm_version + ", " + 126 java_vm_info + ")"); 127 } 128 129 130 /** 131 * Returns the major version of the running JVM if it's 1.6 or newer 132 * or any RE VM build. It will return 0 if it's an internal 1.5 or 133 * 1.4.x build. 134 * 135 * @since 1.6 136 */ 137 public static synchronized int jvmMajorVersion() { 138 if (!versionsInitialized) { 139 initVersions(); 140 } 141 return jvm_major_version; 142 } 143 144 /** 145 * Returns the minor version of the running JVM if it's 1.6 or newer 146 * or any RE VM build. It will return 0 if it's an internal 1.5 or 147 * 1.4.x build. 148 * @since 1.6 149 */ 150 public static synchronized int jvmMinorVersion() { 151 if (!versionsInitialized) { 152 initVersions(); 153 } 154 return jvm_minor_version; 155 } 156 157 158 /** 159 * Returns the micro version of the running JVM if it's 1.6 or newer 160 * or any RE VM build. It will return 0 if it's an internal 1.5 or 161 * 1.4.x build. 162 * @since 1.6 163 */ 164 public static synchronized int jvmMicroVersion() { 165 if (!versionsInitialized) { 166 initVersions(); 167 } 168 return jvm_micro_version; 169 } 170 171 /** 172 * Returns the update release version of the running JVM if it's 173 * a RE build. It will return 0 if it's an internal build. 174 * @since 1.6 175 */ 176 public static synchronized int jvmUpdateVersion() { 177 if (!versionsInitialized) { 178 initVersions(); 179 } 180 return jvm_update_version; 181 } 182 183 public static synchronized String jvmSpecialVersion() { 184 if (!versionsInitialized) { 185 initVersions(); 186 } 187 if (jvm_special_version == null) { 188 jvm_special_version = getJvmSpecialVersion(); 189 } 190 return jvm_special_version; 191 } 192 public static native String getJvmSpecialVersion(); 193 194 /** 195 * Returns the build number of the running JVM if it's a RE build 196 * It will return 0 if it's an internal build. 197 * @since 1.6 198 */ 199 public static synchronized int jvmBuildNumber() { 200 if (!versionsInitialized) { 201 initVersions(); 202 } 203 return jvm_build_number; 204 } 205 206 /** 207 * Returns the major version of the running JDK. 208 * 209 * @since 1.6 210 */ 211 public static synchronized int jdkMajorVersion() { 212 if (!versionsInitialized) { 213 initVersions(); 214 } 215 return jdk_major_version; 216 } 217 218 /** 219 * Returns the minor version of the running JDK. 220 * @since 1.6 221 */ 222 public static synchronized int jdkMinorVersion() { 223 if (!versionsInitialized) { 224 initVersions(); 225 } 226 return jdk_minor_version; 227 } 228 229 /** 230 * Returns the micro version of the running JDK. 231 * @since 1.6 232 */ 233 public static synchronized int jdkMicroVersion() { 234 if (!versionsInitialized) { 235 initVersions(); 236 } 237 return jdk_micro_version; 238 } 239 240 /** 241 * Returns the update release version of the running JDK if it's 242 * a RE build. It will return 0 if it's an internal build. 243 * @since 1.6 244 */ 245 public static synchronized int jdkUpdateVersion() { 246 if (!versionsInitialized) { 247 initVersions(); 248 } 249 return jdk_update_version; 250 } 251 252 public static synchronized String jdkSpecialVersion() { 253 if (!versionsInitialized) { 254 initVersions(); 255 } 256 if (jdk_special_version == null) { 257 jdk_special_version = getJdkSpecialVersion(); 258 } 259 return jdk_special_version; 260 } 261 public static native String getJdkSpecialVersion(); 262 263 /** 264 * Returns the build number of the running JDK if it's a RE build 265 * It will return 0 if it's an internal build. 266 * @since 1.6 267 */ 268 public static synchronized int jdkBuildNumber() { 269 if (!versionsInitialized) { 270 initVersions(); 271 } 272 return jdk_build_number; 273 } 274 275 // true if JVM exports the version info including the capabilities 276 private static boolean jvmVersionInfoAvailable; 277 private static synchronized void initVersions() { 278 if (versionsInitialized) { 279 return; 280 } 281 jvmVersionInfoAvailable = getJvmVersionInfo(); 282 if (!jvmVersionInfoAvailable) { 283 // parse java.vm.version for older JVM before the 284 // new JVM_GetVersionInfo is added. 285 // valid format of the version string is: 286 // n.n.n[_uu[c]][-<identifer>]-bxx 287 CharSequence cs = System.getProperty("java.vm.version"); 288 if (cs.length() >= 5 && 289 Character.isDigit(cs.charAt(0)) && cs.charAt(1) == '.' && 290 Character.isDigit(cs.charAt(2)) && cs.charAt(3) == '.' && 291 Character.isDigit(cs.charAt(4))) { 292 jvm_major_version = Character.digit(cs.charAt(0), 10); 293 jvm_minor_version = Character.digit(cs.charAt(2), 10); 294 jvm_micro_version = Character.digit(cs.charAt(4), 10); 295 cs = cs.subSequence(5, cs.length()); 296 if (cs.charAt(0) == '_' && cs.length() >= 3 && 297 Character.isDigit(cs.charAt(1)) && 298 Character.isDigit(cs.charAt(2))) { 299 int nextChar = 3; 300 try { 301 String uu = cs.subSequence(1, 3).toString(); 302 jvm_update_version = Integer.valueOf(uu).intValue(); 303 if (cs.length() >= 4) { 304 char c = cs.charAt(3); 305 if (c >= 'a' && c <= 'z') { 306 jvm_special_version = Character.toString(c); 307 nextChar++; 308 } 309 } 310 } catch (NumberFormatException e) { 311 // not conforming to the naming convention 312 return; 313 } 314 cs = cs.subSequence(nextChar, cs.length()); 315 } 316 if (cs.charAt(0) == '-') { 317 // skip the first character 318 // valid format: <identifier>-bxx or bxx 319 // non-product VM will have -debug|-release appended 320 cs = cs.subSequence(1, cs.length()); 321 String[] res = cs.toString().split("-"); 322 for (String s : res) { 323 if (s.charAt(0) == 'b' && s.length() == 3 && 324 Character.isDigit(s.charAt(1)) && 325 Character.isDigit(s.charAt(2))) { 326 jvm_build_number = 327 Integer.valueOf(s.substring(1, 3)).intValue(); 328 break; 329 } 330 } 331 } 332 } 333 } 334 getJdkVersionInfo(); 335 versionsInitialized = true; 336 } 337 338 // Gets the JVM version info if available and sets the jvm_*_version fields 339 // and its capabilities. 340 // 341 // Return false if not available which implies an old VM (Tiger or before). 342 private static native boolean getJvmVersionInfo(); 343 private static native void getJdkVersionInfo(); 344 345 // Possible runtime profiles, ordered from small to large 346 private final static String[] PROFILES = { "compact1", "compact2", "compact3" }; 347 348 /** 349 * Returns the name of the profile that this runtime implements. The empty 350 * string is returned for the full Java Runtime. 351 */ 352 public static String profileName() { 353 return java_profile_name; 354 } 355 356 /** 357 * Indicates if this runtime implements the full Java Runtime. 358 */ 359 public static boolean isFullJre() { 360 return java_profile_name.length() == 0; 361 } 362 363 // cached index of this profile's name in PROFILES 364 private static int thisRuntimeIndex = -1; 365 366 /** 367 * Indicates if this runtime supports the given profile. Profile names are 368 * compared without regard to case. Returns {@code false} if the given profile 369 * name is not a supported profile. 370 */ 371 public static boolean supportsProfile(String requiredProfile) { 372 int x = thisRuntimeIndex; 373 if (x < 0) { 374 String profile = profileName(); 375 if (profile.length() > 0) { 376 x = 0; 377 while (x < PROFILES.length) { 378 if (PROFILES[x].equalsIgnoreCase(profile)) 379 break; 380 x++; 381 } 382 if (x >= PROFILES.length) 383 throw new InternalError(profile + " not known to sun.misc.Version"); 384 385 // okay if another thread has already set it 386 thisRuntimeIndex = x; 387 } 388 // else we are a full JRE 389 } 390 391 int y = 0; 392 while (y < PROFILES.length) { 393 if (PROFILES[y].equalsIgnoreCase(requiredProfile)) 394 break; 395 y++; 396 } 397 if (y >= PROFILES.length) { 398 // profile not found so caller has requested something that is not defined 399 return false; 400 } 401 402 return x < 0 || x >= y; 403 } 404 405 } 406 407 // Help Emacs a little because this file doesn't end in .java. 408 // 409 // Local Variables: *** 410 // mode: java *** 411 // End: ***