Skip to content

Commit

Permalink
feat: download and extract javafx if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
lart2150 committed Feb 12, 2023
1 parent 4dd5c01 commit fad9d52
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<project name="kmttg" basedir="." default="all">
<property name="rversion" value="v2.7-rc1-l"/>
<property name="rversion" value="v2.7-rc2-l"/>
<tstamp/>
<property name="bversion" value="${DSTAMP}-${TSTAMP}"/>
<property name="lib" value="lib"/>
Expand Down
127 changes: 122 additions & 5 deletions src/com/tivo/kmttg/gui/JavaFX.java
Original file line number Diff line number Diff line change
@@ -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<String> command = new ArrayList<String>();
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);
}
}
6 changes: 3 additions & 3 deletions src/com/tivo/kmttg/install/update.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/com/tivo/kmttg/main/config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down

0 comments on commit fad9d52

Please sign in to comment.