2
* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
27
import java.io.PrintStream;
29
public class Version {
32
private static final String launcher_name =
35
private static final String java_version =
38
private static final String java_runtime_name =
39
"OpenJDK Runtime Environment";
41
private static final String java_runtime_version =
42
"1.7.0-internal-jeroen_2012_05_22_06_05-b00";
48
public static void init() {
51
private static boolean versionsInitialized = false;
52
private static int jvm_major_version = 0;
53
private static int jvm_minor_version = 0;
54
private static int jvm_micro_version = 0;
55
private static int jvm_update_version = 0;
56
private static int jvm_build_number = 0;
57
private static String jvm_special_version = null;
58
private static int jdk_major_version = 0;
59
private static int jdk_minor_version = 0;
60
private static int jdk_micro_version = 0;
61
private static int jdk_update_version = 0;
62
private static int jdk_build_number = 0;
63
private static String jdk_special_version = null;
66
* In case you were wondering this method is called by java -version.
67
* Sad that it prints to stderr; would be nicer if default printed on
70
public static void print() {
75
* This is the same as print except that it adds an extra line-feed
76
* at the end, typically used by the -showversion in the launcher
78
public static void println() {
84
* Give a stream, it will print version info on it.
86
public static void print(PrintStream ps) {
87
boolean isHeadless = false;
89
/* Report that we're running headless if the property is true */
90
String headless = System.getProperty("java.awt.headless");
91
if ( (headless != null) && (headless.equalsIgnoreCase("true")) ) {
95
/* First line: platform version. */
96
ps.println(launcher_name + " version \"" + java_version + "\"");
98
/* Second line: runtime version (ie, libraries). */
100
ps.print(java_runtime_name + " (build " + java_runtime_version);
102
if (java_runtime_name.indexOf("Embedded") != -1 && isHeadless) {
103
// embedded builds report headless state
104
ps.print(", headless");
108
/* Third line: JVM information. */
109
String java_vm_name = System.getProperty("java.vm.name");
110
String java_vm_version = System.getProperty("java.vm.version");
111
String java_vm_info = System.getProperty("java.vm.info");
112
ps.println(java_vm_name + " (build " + java_vm_version + ", " +
118
* Returns the major version of the running JVM if it's 1.6 or newer
119
* or any RE VM build. It will return 0 if it's an internal 1.5 or
124
public static synchronized int jvmMajorVersion() {
125
if (!versionsInitialized) {
128
return jvm_major_version;
132
* Returns the minor version of the running JVM if it's 1.6 or newer
133
* or any RE VM build. It will return 0 if it's an internal 1.5 or
137
public static synchronized int jvmMinorVersion() {
138
if (!versionsInitialized) {
141
return jvm_minor_version;
146
* Returns the micro version of the running JVM if it's 1.6 or newer
147
* or any RE VM build. It will return 0 if it's an internal 1.5 or
151
public static synchronized int jvmMicroVersion() {
152
if (!versionsInitialized) {
155
return jvm_micro_version;
159
* Returns the update release version of the running JVM if it's
160
* a RE build. It will return 0 if it's an internal build.
163
public static synchronized int jvmUpdateVersion() {
164
if (!versionsInitialized) {
167
return jvm_update_version;
170
public static synchronized String jvmSpecialVersion() {
171
if (!versionsInitialized) {
174
if (jvm_special_version == null) {
175
jvm_special_version = getJvmSpecialVersion();
177
return jvm_special_version;
179
public static native String getJvmSpecialVersion();
182
* Returns the build number of the running JVM if it's a RE build
183
* It will return 0 if it's an internal build.
186
public static synchronized int jvmBuildNumber() {
187
if (!versionsInitialized) {
190
return jvm_build_number;
194
* Returns the major version of the running JDK.
198
public static synchronized int jdkMajorVersion() {
199
if (!versionsInitialized) {
202
return jdk_major_version;
206
* Returns the minor version of the running JDK.
209
public static synchronized int jdkMinorVersion() {
210
if (!versionsInitialized) {
213
return jdk_minor_version;
217
* Returns the micro version of the running JDK.
220
public static synchronized int jdkMicroVersion() {
221
if (!versionsInitialized) {
224
return jdk_micro_version;
228
* Returns the update release version of the running JDK if it's
229
* a RE build. It will return 0 if it's an internal build.
232
public static synchronized int jdkUpdateVersion() {
233
if (!versionsInitialized) {
236
return jdk_update_version;
239
public static synchronized String jdkSpecialVersion() {
240
if (!versionsInitialized) {
243
if (jdk_special_version == null) {
244
jdk_special_version = getJdkSpecialVersion();
246
return jdk_special_version;
248
public static native String getJdkSpecialVersion();
251
* Returns the build number of the running JDK if it's a RE build
252
* It will return 0 if it's an internal build.
255
public static synchronized int jdkBuildNumber() {
256
if (!versionsInitialized) {
259
return jdk_build_number;
262
// true if JVM exports the version info including the capabilities
263
private static boolean jvmVersionInfoAvailable;
264
private static synchronized void initVersions() {
265
if (versionsInitialized) {
268
jvmVersionInfoAvailable = getJvmVersionInfo();
269
if (!jvmVersionInfoAvailable) {
270
// parse java.vm.version for older JVM before the
271
// new JVM_GetVersionInfo is added.
272
// valid format of the version string is:
273
// n.n.n[_uu[c]][-<identifer>]-bxx
274
CharSequence cs = System.getProperty("java.vm.version");
275
if (cs.length() >= 5 &&
276
Character.isDigit(cs.charAt(0)) && cs.charAt(1) == '.' &&
277
Character.isDigit(cs.charAt(2)) && cs.charAt(3) == '.' &&
278
Character.isDigit(cs.charAt(4))) {
279
jvm_major_version = Character.digit(cs.charAt(0), 10);
280
jvm_minor_version = Character.digit(cs.charAt(2), 10);
281
jvm_micro_version = Character.digit(cs.charAt(4), 10);
282
cs = cs.subSequence(5, cs.length());
283
if (cs.charAt(0) == '_' && cs.length() >= 3 &&
284
Character.isDigit(cs.charAt(1)) &&
285
Character.isDigit(cs.charAt(2))) {
288
String uu = cs.subSequence(1, 3).toString();
289
jvm_update_version = Integer.valueOf(uu).intValue();
290
if (cs.length() >= 4) {
291
char c = cs.charAt(3);
292
if (c >= 'a' && c <= 'z') {
293
jvm_special_version = Character.toString(c);
297
} catch (NumberFormatException e) {
298
// not conforming to the naming convention
301
cs = cs.subSequence(nextChar, cs.length());
303
if (cs.charAt(0) == '-') {
304
// skip the first character
305
// valid format: <identifier>-bxx or bxx
306
// non-product VM will have -debug|-release appended
307
cs = cs.subSequence(1, cs.length());
308
String[] res = cs.toString().split("-");
309
for (String s : res) {
310
if (s.charAt(0) == 'b' && s.length() == 3 &&
311
Character.isDigit(s.charAt(1)) &&
312
Character.isDigit(s.charAt(2))) {
314
Integer.valueOf(s.substring(1, 3)).intValue();
322
versionsInitialized = true;
325
// Gets the JVM version info if available and sets the jvm_*_version fields
326
// and its capabilities.
328
// Return false if not available which implies an old VM (Tiger or before).
329
private static native boolean getJvmVersionInfo();
330
private static native void getJdkVersionInfo();
334
// Help Emacs a little because this file doesn't end in .java.
336
// Local Variables: ***