|
| 1 | +package org.scijava.maven.plugin.install; |
| 2 | + |
| 3 | +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; |
| 4 | +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; |
| 5 | +import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; |
| 6 | +import org.apache.commons.io.IOUtils; |
| 7 | + |
| 8 | +import java.io.BufferedInputStream; |
| 9 | +import java.io.File; |
| 10 | +import java.io.FileOutputStream; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.InputStream; |
| 13 | +import java.net.MalformedURLException; |
| 14 | +import java.net.URL; |
| 15 | +import java.nio.file.Files; |
| 16 | +import java.nio.file.Path; |
| 17 | +import java.nio.file.Paths; |
| 18 | + |
| 19 | +public class JavaDownloader { |
| 20 | + |
| 21 | + public static void downloadJava(File destinationDir) throws IOException { |
| 22 | + System.out.println("Downloading and unpacking JRE.."); |
| 23 | + File javaFolder = new File(destinationDir, "java"); |
| 24 | + javaFolder.mkdirs(); |
| 25 | + URL javaURL = getJavaDownloadURL(); |
| 26 | + decompress(javaURL.openStream(), javaFolder); |
| 27 | + File[] files = javaFolder.listFiles(); |
| 28 | + if(files.length != 1) { |
| 29 | + System.err.println("Something went wrong during JRE download"); |
| 30 | + return; |
| 31 | + } |
| 32 | + File jre = files[0]; |
| 33 | + String jdkName = jre.getName().replace("jre", "jdk"); |
| 34 | + Path newJrePath = Paths.get(javaFolder.getAbsolutePath(), getJavaDownloadPlatform(), jdkName, "jre"); |
| 35 | + newJrePath.toFile().getParentFile().mkdirs(); |
| 36 | + Files.move(jre.toPath(), newJrePath); |
| 37 | + System.out.println("JRE installed to " + newJrePath.toAbsolutePath()); |
| 38 | + } |
| 39 | + |
| 40 | + private static void decompress(InputStream in, File out) throws IOException { |
| 41 | + try (TarArchiveInputStream fin = new TarArchiveInputStream( |
| 42 | + new GzipCompressorInputStream(new BufferedInputStream(in)))){ |
| 43 | + TarArchiveEntry entry; |
| 44 | + while ((entry = fin.getNextTarEntry()) != null) { |
| 45 | + if (entry.isDirectory()) { |
| 46 | + continue; |
| 47 | + } |
| 48 | + File curfile = new File(out, entry.getName()); |
| 49 | + File parent = curfile.getParentFile(); |
| 50 | + if (!parent.exists()) { |
| 51 | + parent.mkdirs(); |
| 52 | + } |
| 53 | + IOUtils.copy(fin, new FileOutputStream(curfile)); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private static String getJavaDownloadPlatform() { |
| 59 | + final boolean is64bit = |
| 60 | + System.getProperty("os.arch", "").contains("64"); |
| 61 | + final String osName = System.getProperty("os.name", "<unknown>"); |
| 62 | + if (osName.equals("Linux")) return "linux" + (is64bit ? "-amd64" : ""); |
| 63 | + if (osName.equals("Mac OS X")) return "macosx"; |
| 64 | + if (osName.startsWith("Windows")) return "win" + (is64bit ? "64" : "32"); |
| 65 | + throw new RuntimeException("No JRE for platform exists"); |
| 66 | + } |
| 67 | + |
| 68 | + private static URL getJavaDownloadURL() throws MalformedURLException { |
| 69 | + return new URL("https://downloads.imagej.net/java/" + getJavaDownloadPlatform() + ".tar.gz"); |
| 70 | + } |
| 71 | +} |
0 commit comments