-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava.java
More file actions
62 lines (50 loc) · 1.92 KB
/
Java.java
File metadata and controls
62 lines (50 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package javaxt.utils;
//******************************************************************************
//** Java Class
//******************************************************************************
/**
* Used obtain the version of Java running this library.
*
******************************************************************************/
public class Java {
/** Static variable used to store the output of getJavaVersion() */
public static final int version = getJavaVersion();
//**************************************************************************
//** getVersion
//**************************************************************************
/** Returns the version number of the JVM. Before Java 9, Java releases had
* a "1." prefix (e.g. "1.8"). This method will return the major release
* without the "1." prefix (e.g. 5, 6, 7, 8, 9, 10, ..., 17, etc). Returns
* a -1 if the version could not be determined for whatever reason.
*/
public static int getVersion() {
return version;
}
private static int getJavaVersion(){
int version;
try{
version = getVersion(System.getProperty("java.version"));
}
catch (Throwable t){
try{
version = getVersion(
java.lang.Runtime.class.getMethod("version").invoke(null).toString());
//version = getVersion(java.lang.Runtime.version().toString());
}
catch (Throwable t2){
version = -1;
}
}
return version;
}
private static int getVersion(String version) throws Exception {
if (version.startsWith("1.")) {
version = version.substring(2, 3);
}
else {
int dot = version.indexOf(".");
if (dot != -1) { version = version.substring(0, dot); }
}
return Integer.parseInt(version);
}
}