Skip to content

Commit bb530c0

Browse files
BANSAFAngitbutler-client
authored andcommitted
feat: Add function to get Java version from different providers
Add a function to retrieve the Java version from various Java providers such as Adoptium, Oracle, Amazon Corretto, Azul Zulu, Red Hat OpenJDK, and Microsoft OpenJDK. The function parses the HTML content from specific URLs for each provider and returns the Java version.
1 parent ec354ed commit bb530c0

File tree

3 files changed

+70
-10
lines changed

3 files changed

+70
-10
lines changed

scr/JavaVersionChecker.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
public class JavaVersionChecker {
1212
private static final Map<String, String> SITES = new HashMap<>();
13+
1314
static {
1415
SITES.put("Adoptium", "https://adoptium.net/temurin/releases/?version=17");
1516
SITES.put("Oracle", "https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html");
@@ -39,19 +40,27 @@ private static String checkVersion(String provider, String url) throws IOExcepti
3940
switch (provider) {
4041
case "Adoptium":
4142
Elements releases = doc.select(".release-versions");
42-
version = releases.first().text();
43+
if (!releases.isEmpty()) {
44+
version = releases.first().text();
45+
}
4346
break;
4447
case "Oracle":
4548
Elements links = doc.select("a:contains(jdk-17)");
46-
version = links.first().text().split(" ")[0];
49+
if (!links.isEmpty()) {
50+
version = links.first().text().split(" ")[0];
51+
}
4752
break;
4853
case "Amazon Corretto":
4954
Elements rows = doc.select("table tr");
50-
version = rows.get(1).select("td").get(0).text();
55+
if (rows.size() > 1) {
56+
version = rows.get(1).select("td").get(0).text();
57+
}
5158
break;
5259
case "Azul Zulu":
5360
Elements versions = doc.select(".version-string");
54-
version = versions.first().text();
61+
if (!versions.isEmpty()) {
62+
version = versions.first().text();
63+
}
5564
break;
5665
case "Red Hat OpenJDK":
5766
case "Microsoft OpenJDK":
@@ -68,7 +77,6 @@ private static boolean isUrlUpdated(String url) throws IOException {
6877
long lastModified = connection.getLastModified();
6978
connection.disconnect();
7079

71-
7280
System.out.println("Last modified: " + new java.util.Date(lastModified));
7381

7482
return true;

scr/java.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
function getJavaVersion($url, $provider) {
4+
$ch = curl_init();
5+
6+
curl_setopt($ch, CURLOPT_URL, $url);
7+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
8+
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
9+
10+
$html = curl_exec($ch);
11+
curl_close($ch);
12+
13+
if ($html === false) {
14+
return "Error fetching data from $provider.";
15+
}
16+
switch ($provider) {
17+
case "Adoptium":
18+
preg_match('/<div class="release-versions">.*?<a.*?>(.*?)<\/a>/s', $html, $matches);
19+
return $matches[1] ?? "Version not found";
20+
case "Oracle":
21+
preg_match('/<a.*?jdk-17.*?>(.*?)<\/a>/s', $html, $matches);
22+
return $matches[1] ?? "Version not found";
23+
case "Amazon Corretto":
24+
preg_match('/<td>(.*?)<\/td>/s', $html, $matches);
25+
return $matches[1] ?? "Version not found";
26+
case "Azul Zulu":
27+
preg_match('/<span class="version-string">(.*?)<\/span>/s', $html, $matches);
28+
return $matches[1] ?? "Version not found";
29+
case "Red Hat OpenJDK":
30+
case "Microsoft OpenJDK":
31+
return "Version info not available, please check manually";
32+
default:
33+
return "Unknown provider";
34+
}
35+
}
36+
37+
$sites = [
38+
"Adoptium" => "https://adoptium.net/temurin/releases/?version=17",
39+
"Oracle" => "https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html",
40+
"Amazon Corretto" => "https://docs.aws.amazon.com/corretto/latest/corretto-17-ug/downloads-list.html",
41+
"Azul Zulu" => "https://www.azul.com/downloads/?version=java-17-lts",
42+
"Red Hat OpenJDK" => "https://developers.redhat.com/products/openjdk/download",
43+
"Microsoft OpenJDK" => "https://learn.microsoft.com/en-us/java/openjdk/download"
44+
];
45+
46+
foreach ($sites as $provider => $url) {
47+
$version = getJavaVersion($url, $provider);
48+
echo "$provider: $version\n";
49+
}
50+
?>

scr/pom.xml

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
<dependency>
2-
<groupId>org.jsoup</groupId>
3-
<artifactId>jsoup</artifactId>
4-
<version>1.14.3</version>
5-
</dependency>
1+
<dependencies>
2+
<dependency>
3+
<groupId>org.jsoup</groupId>
4+
<artifactId>jsoup</artifactId>
5+
<version>1.14.3</version>
6+
</dependency>
7+
</dependencies>

0 commit comments

Comments
 (0)