Skip to content

Instantly share code, notes, and snippets.

@TSedlar
Created January 8, 2019 05:25
Show Gist options
  • Save TSedlar/473190f2e143e09bf01e4d5d97cadf3e to your computer and use it in GitHub Desktop.
Save TSedlar/473190f2e143e09bf01e4d5d97cadf3e to your computer and use it in GitHub Desktop.
package rs.toolkit.util.jre;
import rs.toolkit.util.OperatingSystem;
import rs.toolkit.util.io.Internet;
import rs.toolkit.util.io.InternetCallback;
import rs.toolkit.util.io.Untar;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Tyler Sedlar
* @since 3/19/2016
*/
public class JRE32Downloader {
private static final String BASE_URL = "http://download.oracle.com/otn-pub/java/jdk/%s-b02/jre-%s-%s-%s.tar.gz";
private static final String DOWNLOAD_URL = "http://www.java.com/en/download/manual.jsp";
private static final Pattern VERSION_PATTERN = Pattern.compile("Recommended Version (\\d+) Update (\\d+)");
private static final Consumer<HttpURLConnection> LICENSE_CONSUMER =
(connection) -> connection.setRequestProperty("Cookie", "oraclelicense=a;");
public enum JavaSystem {
WINDOWS("windows", "i586"),
LINUX("linux", "i586"),
MAC("macosx", "x64");
public final String system, arch;
JavaSystem(String system, String arch) {
this.system = system;
this.arch = arch;
}
public String resolveURL(String version) {
return String.format(BASE_URL, version, version, system, arch);
}
}
public static boolean download(JavaSystem system, String version, File target) {
if (target.exists()) {
return true;
}
URL url = Internet.resolveURL(system.resolveURL(version), LICENSE_CONSUMER);
return url != null && Internet.download(url.toExternalForm(), target.getAbsolutePath(), new InternetCallback() {
public void onDownload(int percent) {
System.out.println("JRE Install: " + percent + "%");
}
}) != null;
}
public static String latest() {
String source = Internet.readFully(DOWNLOAD_URL);
if (source != null) {
Matcher matcher = VERSION_PATTERN.matcher(source);
if (matcher.find()) {
return (matcher.group(1) + "u" + matcher.group(2));
}
}
return null;
}
public static boolean downloadAndExtract(String version, File target, File directory) {
if (directory.exists()) {
return true;
}
String latest = (version != null ? version : latest());
JavaSystem system = JavaSystem.LINUX;
if (OperatingSystem.get() == OperatingSystem.WINDOWS) {
system = JavaSystem.WINDOWS;
} else if (OperatingSystem.get() == OperatingSystem.MAC) {
system = JavaSystem.MAC;
}
if (download(system, latest, target)) {
List<File> files = Untar.process(target);
if (files != null) {
try {
Files.delete(target.toPath());
} catch (IOException e) {
e.printStackTrace();
return false;
}
for (File file : files) {
if (file.isDirectory()) {
if (file.getName().startsWith("jre")) {
try {
Files.move(file.toPath(), directory.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
}
return true;
}
}
return false;
}
public static boolean downloadAndExtract(File target, File directory) {
return downloadAndExtract(null, target, directory);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment