Skip to content

Instantly share code, notes, and snippets.

@lordofwizard
Forked from AtharvaWaghchoure/Cargo.toml
Last active June 10, 2024 06:08
Show Gist options
  • Save lordofwizard/eb92c4517a9c423a7f116702136be6de to your computer and use it in GitHub Desktop.
Save lordofwizard/eb92c4517a9c423a7f116702136be6de to your computer and use it in GitHub Desktop.
A simple way to download java binaries on Linux Servers using the Adopium horrible API.
[dependencies]
reqwest = { version = "0.11", features = ["blocking", "json"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
use reqwest::blocking::get;
use serde::Deserialize;
use std::process::Command;
#[derive(Deserialize)]
struct AvailableReleases {
available_releases: Vec<u8>,
}
fn download_jdk(java_version: u8) {
// Make the initial API request
let response = match get("https://api.adoptium.net/v3/info/available_releases") {
Ok(res) => res,
Err(_) => {
println!("Failed to make the API request.");
std::process::exit(1);
}
};
// Parse the JSON response
let releases: AvailableReleases = match response.json() {
Ok(json) => json,
Err(_) => {
println!("Failed to parse the JSON response.");
std::process::exit(1);
}
};
// Check if the requested Java version is available
if !releases.available_releases.contains(&java_version) {
println!("not available");
std::process::exit(0);
}
// Replace JAVA_VERSION in the URL
let url = format!(
"https://api.adoptium.net/v3/binary/latest/{}/ga/linux/x64/jdk/hotspot/normal/eclipse",
java_version
);
// Use the CLI tool curl to download the file
let output_file = format!("jdk-{}.tar.gz", java_version);
let status = Command::new("curl")
.arg("-L")
.arg(&url)
.arg("-o")
.arg(&output_file)
.status()
.expect("failed to execute curl command");
if status.success() {
println!("File downloaded successfully as {}", output_file);
} else {
println!("Failed to download the file.");
std::process::exit(1);
}
}
fn main() {
let java_version: u8 = 17; // Replace with desired Java version
download_jdk(java_version);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment