From fad9d52c5c4d096290f71c12e643771a00517c9a Mon Sep 17 00:00:00 2001 From: Brian Engert Date: Sat, 11 Feb 2023 19:32:54 -0600 Subject: [PATCH] feat: download and extract javafx if needed --- build.xml | 2 +- src/com/tivo/kmttg/gui/JavaFX.java | 127 ++++++++++++++++++++++++- src/com/tivo/kmttg/install/update.java | 6 +- src/com/tivo/kmttg/main/config.java | 2 +- 4 files changed, 127 insertions(+), 10 deletions(-) diff --git a/build.xml b/build.xml index 2ba8bf5e..edc36c38 100644 --- a/build.xml +++ b/build.xml @@ -1,6 +1,6 @@ - + diff --git a/src/com/tivo/kmttg/gui/JavaFX.java b/src/com/tivo/kmttg/gui/JavaFX.java index 97cee488..24c676be 100644 --- a/src/com/tivo/kmttg/gui/JavaFX.java +++ b/src/com/tivo/kmttg/gui/JavaFX.java @@ -1,20 +1,137 @@ package com.tivo.kmttg.gui; import javafx.scene.paint.Color; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; + import javax.swing.JOptionPane; +import com.tivo.kmttg.install.update; +import com.tivo.kmttg.util.file; +import com.tivo.kmttg.util.log; public class JavaFX { - public static final String message = "KMTTG requires JavaFX. Please use either the oracle jre or a opendjk with JavaFX."; - - public static boolean checkForJavaFX() - { + public static final String message = "KMTTG requires JavaFX. Please use either the oracle jre 8 or extract the javafx sdk in javafx-sdk folder"; + + public static boolean checkForJavaFX() { try { - Color c = javafx.scene.paint.Color.BLUE; + @SuppressWarnings("unused") + Color c = javafx.scene.paint.Color.BLUE; } catch (NoClassDefFoundError e) { + try { + final File currentJar = new File( + JavaFX.class.getProtectionDomain().getCodeSource().getLocation().toURI()); + String javaFXPath = findJavaFXLib(currentJar); + if (javaFXPath != null) { + restartWithJavaFx(currentJar, javaFXPath); + } else { + int answer = JOptionPane.showConfirmDialog(null, "You are missing javfx. Would you like to download it now?", "Missing JavaFX", JOptionPane.YES_NO_OPTION); + if (answer == JOptionPane.YES_OPTION) { + downloadJavaFX(currentJar); + } else { + return false; + } + } + } catch (Exception e1) { + log.error(e1.getMessage()); + } + JOptionPane.showMessageDialog(null, message, "Missing JavaFX", JOptionPane.ERROR_MESSAGE); return false; } return true; } + + private static boolean downloadJavaFX(File currentJar) throws IOException { + String arch = System.getProperty("os.arch"); + String os = System.getProperty("os.name").toLowerCase(); + String downloadOs = ""; + if (os.contains("windows")) { + downloadOs = "windows"; + } else if (os.contains("mac")) { + downloadOs = "osx"; + } else if (os.contains("linux")) { + downloadOs = "linux"; + } else { + return false; + } + + switch (arch) { + case "amd64": + case "x86_64": + arch = "x64"; + break; + } + String zipName = currentJar.getParent() + "/openfx-sdk.zip"; + String downloadLink = "https://download2.gluonhq.com/openjfx/19.0.2.1/openjfx-19.0.2.1_" +downloadOs+ "-" + arch + "_bin-sdk.zip"; + String zipFile = update.downloadUrl(zipName, downloadLink); + if (zipFile != null) { + File sdkDir = new File(currentJar.getParent() + "/javafx-sdk/"); + if (!sdkDir.exists()) { + sdkDir.mkdir(); + } + if ( update.unzip(sdkDir.toString(), zipFile) ) { + log.print("Successfully download javafx."); + file.delete(zipFile); + String javaFXPath = findJavaFXLib(currentJar); + restartWithJavaFx(currentJar, javaFXPath); + } else { + log.error("Trouble unzipping file: " + zipFile); + } + } else { + JOptionPane.showMessageDialog(null, "We failed to download javafx.", "JavaFX Download Failed", JOptionPane.ERROR_MESSAGE); + } + + return false; + } + + private static String findJavaFXLib(File currentJar) { + File javaFXpath = new File(currentJar.getParent() + "/javafx-sdk/"); + if (!javaFXpath.exists()) { + System.out.println("Javafx folder is not there: " + javaFXpath.toString()); + return null; + } + + File[] listOfFiles = javaFXpath.listFiles(); + for (File file : listOfFiles) { + System.out.println(file.getName()); + if (file.getName().equals("lib")) { + return file.toString(); + } + if (file.getName().contains("javafx")) { + File[] fxFiles = file.listFiles(); + for (File fxfile : fxFiles) { + System.out.println(fxfile.getName()); + if (fxfile.getName().equals("lib")) { + return fxfile.toString(); + } + } + } + } + return null; + } + + private static void restartWithJavaFx(File currentJar, String javaFXPath) throws IOException { + + + final String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + + "java"; + + /* Build command: java -jar application.jar */ + final ArrayList command = new ArrayList(); + command.add(javaBin); + command.add("--module-path"); + command.add(javaFXPath); + command.add("--add-modules"); + command.add("javafx.web"); + command.add("-jar"); + command.add(currentJar.getPath()); + + final ProcessBuilder builder = new ProcessBuilder(command); + System.out.println("restarting with javafx"); + builder.start(); + System.exit(0); + } } diff --git a/src/com/tivo/kmttg/install/update.java b/src/com/tivo/kmttg/install/update.java index b3d82524..e98857d2 100755 --- a/src/com/tivo/kmttg/install/update.java +++ b/src/com/tivo/kmttg/install/update.java @@ -180,7 +180,7 @@ public backgroundRun(String version) { } @SuppressWarnings("resource") - private static String downloadUrl(String localFileName, String urlString) { + public static String downloadUrl(String localFileName, String urlString) { BufferedInputStream in = null; RandomAccessFile out = null; int BLOCK_SIZE = 4096; @@ -230,7 +230,7 @@ private static String downloadUrl(String localFileName, String urlString) { } @SuppressWarnings("resource") - private static Boolean unzip(String dir, String file) { + public static Boolean unzip(String dir, String file) { Enumeration entries; ZipFile zipFile; @@ -259,7 +259,7 @@ private static Boolean unzip(String dir, String file) { zipFile.close(); // Set all files as executable if non-Windows platform - if ( ! config.OS.equals("windows") ) { + if ( ! System.getProperty("os.name").toLowerCase().contains("windows") ) { String[] command = new String[] {"chmod", "-R", "ugo+x", dir}; Runtime.getRuntime().exec(command); } diff --git a/src/com/tivo/kmttg/main/config.java b/src/com/tivo/kmttg/main/config.java index d3261dc2..713f7dc1 100644 --- a/src/com/tivo/kmttg/main/config.java +++ b/src/com/tivo/kmttg/main/config.java @@ -38,7 +38,7 @@ import com.tivo.kmttg.httpserver.kmttgServer; public class config { - public static String kmttg = "kmttg v2.7-rc1-l"; + public static String kmttg = "kmttg v2.7-rc2-l"; // encoding related public static String encProfDir = "";