1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.utils;
19
20 import org.apache.commons.lang3.StringUtils;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24
25
26
27
28 public final class Utils {
29
30
31
32
33 private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class);
34
35
36
37
38 private Utils() {
39 }
40
41
42
43
44
45
46 public static int getJavaVersion() {
47 String version = System.getProperty("java.specification.version");
48 if (version.startsWith("1.")) {
49 version = version.substring(2, 3);
50 } else {
51 final int dot = version.indexOf(".");
52 if (dot != -1) {
53 version = version.substring(0, dot);
54 }
55 }
56 return Integer.parseInt(version);
57 }
58
59
60
61
62
63
64 public static int getJavaUpdateVersion() {
65
66 String runtimeVersion = System.getProperty("java.version");
67 return parseUpdate(runtimeVersion);
68 }
69
70
71
72
73
74
75
76 protected static int parseUpdate(String runtimeVersion) {
77 LOGGER.debug(runtimeVersion);
78 try {
79 String[] parts = runtimeVersion.split("\\.");
80 if (parts.length == 4 && isNumeric(parts)) {
81 return Integer.parseInt(parts[2]);
82 }
83 int pos = runtimeVersion.indexOf('_');
84 if (pos <= 0) {
85 pos = runtimeVersion.lastIndexOf('.');
86 if (pos <= 0) {
87
88 return 0;
89 }
90 }
91 int end = runtimeVersion.indexOf('+', pos);
92 if (end < 0) {
93 end = runtimeVersion.indexOf('-', pos);
94 }
95 if (end > pos) {
96 return Integer.parseInt(runtimeVersion.substring(pos + 1, end));
97 }
98 return Integer.parseInt(runtimeVersion.substring(pos + 1));
99 } catch (NumberFormatException nfe) {
100
101 return 0;
102 }
103 }
104
105
106
107
108
109
110
111
112 private static boolean isNumeric(String[] parts) {
113 for (String i : parts) {
114 if (!StringUtils.isNumeric(i)) {
115 return false;
116 }
117 }
118 return true;
119 }
120
121 public static void main(String[] args) {
122 System.out.println("Java runtime : " + System.getProperty("java.runtime.version"));
123 System.out.println("Java version : " + getJavaVersion());
124 System.out.println("Java update : " + getJavaUpdateVersion());
125
126 }
127 }