aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea
diff options
context:
space:
mode:
authornea <nea@nea.moe>2023-11-01 23:04:25 +0100
committernea <nea@nea.moe>2023-11-01 23:04:25 +0100
commit2d1bf11103b2635181ecb64c9be0638714328fe0 (patch)
treed548c12188a40a48961c18636ab0ef54e6fcac26 /src/main/java/moe/nea
parent21ca764e26fdbb903ce380874cc2798f17de9034 (diff)
downloadModernJavaLauncher-2d1bf11103b2635181ecb64c9be0638714328fe0.tar.gz
ModernJavaLauncher-2d1bf11103b2635181ecb64c9be0638714328fe0.tar.bz2
ModernJavaLauncher-2d1bf11103b2635181ecb64c9be0638714328fe0.zip
Add java binary finder
Diffstat (limited to 'src/main/java/moe/nea')
-rw-r--r--src/main/java/moe/nea/modernjava/launch/relaunch/FCPRelauncher.java14
-rw-r--r--src/main/java/moe/nea/modernjava/launch/relaunch/JavaScanner.java137
-rw-r--r--src/main/java/moe/nea/modernjava/launch/util/PropertyNames.java5
3 files changed, 155 insertions, 1 deletions
diff --git a/src/main/java/moe/nea/modernjava/launch/relaunch/FCPRelauncher.java b/src/main/java/moe/nea/modernjava/launch/relaunch/FCPRelauncher.java
index fe74c42..3e13806 100644
--- a/src/main/java/moe/nea/modernjava/launch/relaunch/FCPRelauncher.java
+++ b/src/main/java/moe/nea/modernjava/launch/relaunch/FCPRelauncher.java
@@ -46,7 +46,19 @@ public class FCPRelauncher {
}
public static File findJavaLauncher() {
- return new File("/home/nea/.sdkman/candidates/java/16.0.2-tem/bin/java");
+ JavaScanner javaScanner = new JavaScanner();
+ javaScanner.scanDefaultPaths();
+ javaScanner.prettyPrint();
+ JavaScanner.LocalJavaVersion candidate = javaScanner.findCandidate();
+ if (candidate == null) {
+ System.err.println("Looks like we couldn't find a java candidate. Either install one, or if you have one" +
+ " and we just cannot find it, specify -D" + PropertyNames.JAVA_SCAN_PATH + "=<java home here>." +
+ " We need a Java 16 JDK. Exiting now.");
+ IAMFML.shutdown(1);
+ throw new RuntimeException();
+ }
+ System.out.println("Choosing Java Candidate:\n" + candidate.prettyPrint());
+ return candidate.getJavaBinary();
}
public static File findAgentJar() {
diff --git a/src/main/java/moe/nea/modernjava/launch/relaunch/JavaScanner.java b/src/main/java/moe/nea/modernjava/launch/relaunch/JavaScanner.java
new file mode 100644
index 0000000..0003097
--- /dev/null
+++ b/src/main/java/moe/nea/modernjava/launch/relaunch/JavaScanner.java
@@ -0,0 +1,137 @@
+package moe.nea.modernjava.launch.relaunch;
+
+import moe.nea.modernjava.launch.util.PropertyNames;
+import moe.nea.modernjava.launch.util.TextIoUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.SystemUtils;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+public class JavaScanner {
+
+ public static final String JAVA_BINARY_PATH = "bin/java" + (SystemUtils.IS_OS_WINDOWS ? ".exe" : "");
+ public static final String JAVA_COMPILER_PATH = "bin/javac" + (SystemUtils.IS_OS_WINDOWS ? ".exe" : "");
+
+ public boolean isJavaHome(File potential) {
+ if (new File(potential, JAVA_BINARY_PATH).exists()) {
+ return true;
+ }
+ return false;
+ }
+
+ private List<LocalJavaVersion> versionList = new ArrayList<>();
+
+ public void scanDirectory(File file) {
+ scanDirectory(file, 1);
+ }
+
+ public void scanDirectory(File file, int depthLimit) {
+ if (depthLimit < 0) return;
+ if (isJavaHome(file)) {
+ versionList.add(new LocalJavaVersion(file));
+ } else {
+ for (File listFile : file.listFiles()) {
+ if (listFile.isDirectory()) {
+ scanDirectory(listFile, depthLimit - 1);
+ }
+ }
+ }
+ }
+
+ public void prettyPrint() {
+ String s = "Fun fact these are the found Java Runtime Environments:\n";
+ for (LocalJavaVersion localJavaVersion : versionList) {
+ s += localJavaVersion.prettyPrint();
+ }
+ System.out.println(s);
+ }
+
+ public void scanDefaultPaths() {
+ File home = new File(System.getProperty("user.home"));
+ scanDirectory(new File(home, ".sdkman/candidates/java"));
+ scanDirectory(new File(home, ".jdks"));
+ scanDirectory(new File("/usr"), 0);
+ String[] paths = System.getProperty(PropertyNames.JAVA_SCAN_PATH, "").split(File.pathSeparator);
+ for (String path : paths) {
+ if (!path.isEmpty()) {
+ scanDirectory(new File(path).getParentFile(), 3);
+ }
+ }
+ }
+
+ public LocalJavaVersion findCandidate() {
+ LocalJavaVersion bestFit = null;
+ for (LocalJavaVersion localJavaVersion : versionList) {
+ if (localJavaVersion.isJdk() && localJavaVersion.getMajorVersion() == 16) {
+ bestFit = localJavaVersion;
+ }
+ }
+ return bestFit;
+ }
+
+ public static class LocalJavaVersion {
+ private final File javaHome;
+ private String versionString;
+
+ public LocalJavaVersion(File javaHome) {
+ this.javaHome = javaHome;
+ }
+
+ public File getJavaHome() {
+ return javaHome;
+ }
+
+ public File getJavaBinary() {
+ return new File(javaHome, JAVA_BINARY_PATH);
+ }
+
+ public boolean isJdk() {
+ return new File(javaHome, JAVA_COMPILER_PATH).exists();
+ }
+
+ public String getVersionString() {
+ if (versionString == null) {
+ ProcessBuilder processBuilder = new ProcessBuilder();
+ processBuilder.command(getJavaBinary().getAbsolutePath(), "-version");
+ processBuilder.redirectInput(ProcessBuilder.Redirect.PIPE);
+ processBuilder.redirectOutput(ProcessBuilder.Redirect.PIPE);
+ processBuilder.redirectError(ProcessBuilder.Redirect.PIPE);
+ try {
+ Process process = processBuilder.start();
+ process.waitFor();
+ versionString = IOUtils.toString(process.getInputStream()) + IOUtils.toString(process.getErrorStream());
+ } catch (Exception e) {
+ e.printStackTrace();
+ versionString = "<invalid>";
+ }
+ }
+ return versionString;
+ }
+
+ public String prettyPrint() {
+ return javaHome.getAbsolutePath() + ":\n"
+ + "\tJava Binary: " + getJavaBinary().getAbsolutePath() + "\n"
+ + "\tMajor Version: " + getMajorVersion() + "\n"
+ + "\tFull Version: " + getVersion() + "\n"
+ + "\tIs Jdk: " + isJdk() + "\n"
+ ;
+ }
+
+ public String getVersion() {
+ String v = getVersionString();
+ String[] s = v.split("\"");
+ if (s.length < 2) return null;
+ return s[1];
+ }
+
+ public int getMajorVersion() {
+ try {
+ return Integer.parseInt(getVersion().split("\\.")[0]);
+ } catch (Exception e) {
+ return -1;
+ }
+ }
+ }
+}
diff --git a/src/main/java/moe/nea/modernjava/launch/util/PropertyNames.java b/src/main/java/moe/nea/modernjava/launch/util/PropertyNames.java
index 8540dde..22d3b20 100644
--- a/src/main/java/moe/nea/modernjava/launch/util/PropertyNames.java
+++ b/src/main/java/moe/nea/modernjava/launch/util/PropertyNames.java
@@ -13,4 +13,9 @@ public class PropertyNames {
* Starts a debugger on the given port if present.
*/
public static final String DEBUG_PORT = "modernjava.debugport";
+
+ /**
+ * Scans the given directories for java binaries.
+ */
+ public static final String JAVA_SCAN_PATH = "modernjava.scanpath";
}