Now can choose Java Installation in Windows
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.utils.system;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
@@ -23,6 +24,7 @@ import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
@@ -33,6 +35,7 @@ import java.net.SocketException;
|
||||
import java.net.URL;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.jackhuang.hellominecraft.HMCLog;
|
||||
|
||||
/**
|
||||
@@ -164,7 +167,11 @@ public class IOUtils {
|
||||
}
|
||||
|
||||
public static String getJavaDir() {
|
||||
String path = System.getProperty("java.home") + File.separatorChar + "bin" + File.separatorChar;
|
||||
return getJavaDir(System.getProperty("java.home"));
|
||||
}
|
||||
|
||||
public static String getJavaDir(String home) {
|
||||
String path = home + File.separatorChar + "bin" + File.separatorChar;
|
||||
path = addSeparator(path);
|
||||
if (OS.os() == OS.WINDOWS && new File(path + "javaw.exe").isFile())
|
||||
return path + "javaw.exe";
|
||||
@@ -237,7 +244,7 @@ public class IOUtils {
|
||||
public static String tryGetCanonicalFolderPath(File file) {
|
||||
try {
|
||||
return IOUtils.addSeparator(file.getCanonicalPath());
|
||||
} catch (IOException ex) {
|
||||
} catch (IOException ignored) {
|
||||
return IOUtils.addSeparator(file.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
@@ -245,7 +252,7 @@ public class IOUtils {
|
||||
public static File tryGetCanonicalFile(File file) {
|
||||
try {
|
||||
return file.getCanonicalFile();
|
||||
} catch (IOException ex) {
|
||||
} catch (IOException ignored) {
|
||||
return file.getAbsoluteFile();
|
||||
}
|
||||
}
|
||||
@@ -257,7 +264,7 @@ public class IOUtils {
|
||||
public static String tryGetCanonicalFilePath(File file) {
|
||||
try {
|
||||
return file.getCanonicalPath();
|
||||
} catch (IOException ex) {
|
||||
} catch (IOException ignored) {
|
||||
return file.getAbsolutePath();
|
||||
}
|
||||
}
|
||||
@@ -270,4 +277,30 @@ public class IOUtils {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> readProcessByInputStream(String[] cmd) throws IOException, InterruptedException {
|
||||
JavaProcess jp = new JavaProcess(cmd, new ProcessBuilder(cmd).start(), null);
|
||||
ArrayList<String> lines = new ArrayList<>();
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(jp.getRawProcess().getInputStream()))) {
|
||||
jp.getRawProcess().waitFor();
|
||||
String line;
|
||||
while((line = br.readLine()) != null) {
|
||||
lines.add(line);
|
||||
}
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
public static List<String> readProcessByErrorStream(String[] cmd) throws IOException, InterruptedException {
|
||||
JavaProcess jp = new JavaProcess(cmd, new ProcessBuilder(cmd).start(), null);
|
||||
ArrayList<String> lines = new ArrayList<>();
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(jp.getRawProcess().getErrorStream()))) {
|
||||
jp.getRawProcess().waitFor();
|
||||
String line;
|
||||
while((line = br.readLine()) != null) {
|
||||
lines.add(line);
|
||||
}
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright 2013 huangyuhui <huanghongxun2008@126.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program.
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.utils.system;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.jackhuang.hellominecraft.HMCLog;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public class Java {
|
||||
String name, home;
|
||||
|
||||
public Java(String name, String home) {
|
||||
this.name = name;
|
||||
this.home = home;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getHome() {
|
||||
return home;
|
||||
}
|
||||
|
||||
public String getJava() {
|
||||
return IOUtils.getJavaDir(getHome());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if(obj instanceof Java) {
|
||||
Java j = (Java) obj;
|
||||
return (j.getName() == null && this.getName() == null) || ((Java) obj).getName().equals(this.getName());
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
}
|
||||
|
||||
public static List<Java> queryAllJavaHomeInWindowsByReg() {
|
||||
try {
|
||||
List<Java> ans = new ArrayList<>();
|
||||
List<String> javas = queryRegSubFolders("HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment");
|
||||
for (String java : javas) {
|
||||
int s = 0;
|
||||
for (char c : java.toCharArray())
|
||||
if (c == '.') s++;
|
||||
if (s <= 1) continue;
|
||||
String javahome = queryRegValue(java, "JavaHome");
|
||||
if (javahome != null)
|
||||
ans.add(new Java(java.substring("HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\".length()), javahome));
|
||||
}
|
||||
return ans;
|
||||
} catch (IOException | InterruptedException ex) {
|
||||
HMCLog.err("Faield to query java", ex);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static List<String> queryRegSubFolders(String location) throws IOException, InterruptedException {
|
||||
String[] cmd = new String[]{"cmd", "/c", "reg", "query", location};
|
||||
List<String> l = IOUtils.readProcessByInputStream(cmd);
|
||||
List<String> ans = new ArrayList<>();
|
||||
for (String line : l)
|
||||
if (line.startsWith(location) && !line.equals(location))
|
||||
ans.add(line);
|
||||
return ans;
|
||||
}
|
||||
|
||||
private static String queryRegValue(String location, String name) throws IOException, InterruptedException {
|
||||
String[] cmd = new String[]{"cmd", "/c", "reg", "query", location, "/v", name};
|
||||
List<String> l = IOUtils.readProcessByInputStream(cmd);
|
||||
if (l.size() < 3) return null;
|
||||
// 18 = 4 spaces + [name.length()] + 4 spaces + "REG_SZ".length()=6 characters + 4 spaces
|
||||
return l.get(2).substring(18 + name.length());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,11 +16,8 @@
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.utils.system;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import org.jackhuang.hellominecraft.HMCLog;
|
||||
@@ -154,21 +151,14 @@ public final class JdkVersion {
|
||||
return jdkBit.contains("64");
|
||||
}
|
||||
|
||||
static Pattern p = Pattern.compile("java version \"[1-9]*\\.[1-9]*\\.[0-9]*(.*?)\"");
|
||||
private static final Pattern p = Pattern.compile("java version \"[1-9]*\\.[1-9]*\\.[0-9]*(.*?)\"");
|
||||
|
||||
public static JdkVersion getJavaVersionFromExecutable(String file) throws IOException {
|
||||
String[] str = new String[]{file, "-version"};
|
||||
ProcessBuilder pb = new ProcessBuilder(str);
|
||||
JavaProcess jp = new JavaProcess(str, pb.start(), null);
|
||||
InputStream is = jp.getRawProcess().getErrorStream();
|
||||
BufferedReader br = null;
|
||||
String ver = null;
|
||||
Platform platform = Platform.BIT_32;
|
||||
String ver = null;
|
||||
try {
|
||||
br = new BufferedReader(new InputStreamReader(is));
|
||||
String line;
|
||||
jp.getRawProcess().waitFor();
|
||||
while ((line = br.readLine()) != null) {
|
||||
for(String line : IOUtils.readProcessByErrorStream(str)) {
|
||||
Matcher m = p.matcher(line);
|
||||
if (m.find()) {
|
||||
ver = m.group();
|
||||
@@ -179,9 +169,6 @@ public final class JdkVersion {
|
||||
}
|
||||
} catch (InterruptedException | IOException e) {
|
||||
HMCLog.warn("Failed to get java version", e);
|
||||
} finally {
|
||||
if (br != null)
|
||||
br.close();
|
||||
}
|
||||
return new JdkVersion(file, ver, platform);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user