Skip to content

Instantly share code, notes, and snippets.

@flleeppyy
Created July 8, 2022 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flleeppyy/abc35b1cc3aca0c9bdf5b86d4710ada1 to your computer and use it in GitHub Desktop.
Save flleeppyy/abc35b1cc3aca0c9bdf5b86d4710ada1 to your computer and use it in GitHub Desktop.
deleted source for some java shit
package com.flleeppyy.serverinstaller.Utils;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.prefs.Preferences;
public class JavaUtils {
static class JavaCheck {
private String osArch;
private String javaVersion;
private String javaVendor;
public String getOsArch() {
return osArch;
}
public void setOsArch(String osArch) {
this.osArch = osArch;
}
public String getJavaVersion() {
return javaVersion;
}
public void setJavaVersion(String javaVersion) {
this.javaVersion = javaVersion;
}
public String getJavaVendor() {
return javaVendor;
}
public void setJavaVendor(String javaVendor) {
this.javaVendor = javaVendor;
}
}
static class JavaInstall {
// bool operator<(const JavaInstall & rhs);
// bool operator==(const JavaInstall & rhs);
// bool operator>(const JavaInstall & rhs);
//
// JavaVersion id;
// QString arch;
// QString path;
// bool recommended = false;
// Port to Java
String id;
String path;
String arch;
public JavaInstall(String path, String id, String arch) {
this.path = path;
this.id = id;
this.arch = arch;
}
public JavaInstall(String path, String id) {
this.path = path;
this.id = id;
}
public String detectExecutableArch() {
if (System.getProperty("os.name").contains("win")) {
try {
Path path = Paths.get(this.path);
byte[] data = Files.readAllBytes(path);
byte PEPointer = data[0x3C]; // Returning the PE pointer of the file
byte indexOfShit = ((byte) OtherUtils.indexOf(data, data[PEPointer]));
byte[] archbytes = new byte[4];
for (int i = 0; i < 4; i++) {
archbytes[i] = data[indexOfShit + i];
}
if (archbytes[0] == 0x50 && archbytes[1] == 0x45 && archbytes[2] == 0x00 && archbytes[3] == 0x00) {
return "x86";
} else if (archbytes[0] == 0x50 && archbytes[1] == 0x00 && archbytes[2] == 0x00 && archbytes[3] == 0x00) {
return "x64";
} else {
return null;
}
} catch (Exception ignored) {
}
} else if (System.getProperty("os.name").contains("mac")) {
return "no i hate apple";
} else if (System.getProperty("os.name").contains("linux")) {
} else {
return null;
}
}
public String getPath() {
return path;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getArch() {
return arch;
}
public void setArch(String arch) {
this.arch = arch;
}
public JavaInstall(String path) {
this.path = path;
}
public String toString() {
return String.format("%s %s", id, arch);
}
};
public static String[] getJavaPaths() {
if (System.getProperty("os.name").toLowerCase().contains("windows")) {
// Get path from registry
Preferences system = Preferences.systemRoot();
ArrayList<JavaInstall> javaCandidates = new ArrayList<>();
String[][] javaPaths = {
// Original argument format:
// QString keyName, QString keyJavaDir, QString subkeySuffix
// Oracle
{"SOFTWARE\\JavaSoft\\Java Runtime Environment", "JavaHome"},
{"SOFTWARE\\JavaSoft\\Java Development Kit", "JavaHome"},
// Oracle for Java 9 and newer
{"SOFTWARE\\JavaSoft\\JRE", "JavaHome"},
{"SOFTWARE\\JavaSoft\\JDK", "JavaHome"},
// AdoptOpenJDK
{"SOFTWARE\\AdoptOpenJDK\\JRE", "Path", "\\hotspot\\MSI"},
{"SOFTWARE\\AdoptOpenJDK\\JDK", "Path", "\\hotspot\\MSI"},
// Eclipse Foundation
{"SOFTWARE\\Eclipse Foundation\\JDK", "Path", "\\hotspot\\MSI"},
// Eclipse Adoptium
{"SOFTWARE\\Eclipse Adoptium\\JRE", "Path", "\\hotspot\\MSI"},
{"SOFTWARE\\Eclipse Adoptium\\JDK", "Path", "\\hotspot\\MSI"},
// Microsoft
{"SOFTWARE\\Microsoft\\JDK", "Path", "\\hotspot\\MSI"},
// Azul Zulu
{"SOFTWARE\\Azul Systems\\Zulu", "InstallationPath"},
// BellSoft Liberica
{"SOFTWARE\\BellSoft\\Liberica", "InstallationPath"},
};
javaCandidates.add(new JavaInstall("C:/Program Files/Java/jre8/bin/javaw.exe"));
javaCandidates.add(new JavaInstall("C:/Program Files/Java/jre7/bin/javaw.exe"));
javaCandidates.add(new JavaInstall("C:/Program Files/Java/jre6/bin/javaw.exe"));
javaCandidates.add(new JavaInstall("C:/Program Files (x86)/Java/jre8/bin/javaw.exe"));
javaCandidates.add(new JavaInstall("C:/Program Files (x86)/Java/jre7/bin/javaw.exe"));
javaCandidates.add(new JavaInstall("C:/Program Files (x86)/Java/jre6/bin/javaw.exe"));
for (String[] javaPath : javaPaths) {
for (int i = 0; i < javaPath.length; i++) {
String keyName = javaPath[0];
String keyJavaDir = javaPath[1];
String subkeySuffix = javaPath[2];
String javaPathString = system.get(keyName + "\\" + keyJavaDir + "\\" + subkeySuffix, null);
if (javaPathString != null) {
javaCandidates.add(new JavaInstall(javaPathString));
}
}
}
// List x64 before x86
javaCandidates.sort((a, b) -> {
if (a.getArch().equals("x64") && !b.getArch().equals("x64")) {
return 1;
} else if (b.getArch().equals("x64") && !a.getArch().equals("x64")) {
return -1;
} else {
return 0;
}
});
}
}
private static JavaCheck getJavaVersion(Path javaPath) {
Path javaCheckTemp;
if (System.getProperty("os.name").toLowerCase().contains("windows")) {
javaCheckTemp = Paths.get(System.getProperty("user.home")).resolve("AppData").resolve("Local").resolve("Temp").resolve("JavaCheck.jar");
} else if (System.getProperty("os.name").toLowerCase().contains("mac")) {
javaCheckTemp = Paths.get(System.getProperty("user.home")).resolve("Library").resolve("Caches").resolve("JavaCheck.jar");
} else {
// Linux
// use /tmp
javaCheckTemp = Paths.get("/tmp").resolve("JavaCheck.jar");
}
try {
if (!Files.exists(javaCheckTemp)) {
// copy JavaCheck.jar to temp
Files.copy(JavaUtils.class.getResourceAsStream("/JavaCheck.jar"), javaCheckTemp);
}
// run java checker
ProcessBuilder pb = new ProcessBuilder(javaPath.resolve("java.exe").toString(), "-jar", String.format("\"%s\"", javaCheckTemp));
Process p = pb.start();
p.waitFor();
// read output
String output;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8))) {
output = reader.lines().reduce("", (a, b) -> a + b);
}
return parseJavaCheck(output);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return null;
}
public static String[] findJavaPathsWindows() {
}
public static String[] findJavaPathsMac() {
}
public static String[] findJavaPathsLinux() {
}
/**
* example output:
* os.arch=amd64
* java.version=18.0.1
* java.vendor=Oracle Corporation
*/
public static JavaCheck parseJavaCheck(String input) {
JavaCheck javaCheck = new JavaCheck();
String[] lines = input.split("\n");
for (String line : lines) {
String[] split = line.split("=");
if (split.length == 2) {
String key = split[0];
String value = split[1];
switch (key) {
case "os.arch":
javaCheck.setOsArch(value);
break;
case "java.version":
javaCheck.setJavaVersion(value);
break;
case "java.vendor":
javaCheck.setJavaVendor(value);
break;
}
}
}
return javaCheck;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment