Now can choose Java Installation in Windows

This commit is contained in:
huanghongxun
2015-08-04 15:13:04 +08:00
parent 4a167253db
commit 95598df273
15 changed files with 887 additions and 625 deletions

View File

@@ -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;
}
}

View File

@@ -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());
}
}

View File

@@ -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);
}

View File

@@ -73,6 +73,7 @@ login.not_email=\u7528\u6237\u540d\u5fc5\u987b\u662f\u90ae\u7bb1
login.type=\u767b\u5f55
login.username=\u540d\u5b57
login.account=\u90ae\u7bb1
login.no_valid_character=\u65e0\u6709\u6548\u7684\u89d2\u8272\uff0c\u81ea\u884c\u5230skinme.cc\u767b\u9646\u5e76\u521b\u5efa\u89d2\u8272
proxy.username=\u8d26\u6237
proxy.password=\u5bc6\u7801
@@ -192,6 +193,8 @@ settings.fullscreen=\u5168\u5c4f
settings.update_version=\u66f4\u65b0\u7248\u672c\u6587\u4ef6
settings.physical_memory=\u7269\u7406\u5185\u5b58\u5927\u5c0f
settings.choose_javapath=\u9009\u62e9Java\u8def\u5f84
settings.default=\u9ed8\u8ba4
settings.custom=\u81ea\u5b9a\u4e49
settings.failed_load=\u8bbe\u7f6e\u6587\u4ef6\u52a0\u8f7d\u5931\u8d25\uff0c\u53ef\u80fd\u662f\u5347\u7ea7\u4e86\u542f\u52a8\u5668\u6216\u88ab\u4eba\u5de5\u4fee\u6539\u9020\u6210\u9519\u8bef\uff0c\u662f\u5426\u6e05\u9664\uff1f

View File

@@ -73,6 +73,7 @@ login.not_email=The username must be a e-mail.
login.type=Login
login.username=Name
login.account=Email
login.no_valid_character=No Valid Character, please visit skinme.cc and create your own character.
login.failed.connect_authentication_server=Cannot connect the authentication server. Check your network.
@@ -187,6 +188,8 @@ settings.fullscreen=Fullscreen
settings.update_version=Update version json.
settings.physical_memory=Physical Memory Size
settings.choose_javapath=Choose Java Directory.
settings.default=Default
settings.custom=Custom
settings.failed_load=Failed to load settings file. Remove it?

View File

@@ -73,6 +73,7 @@ login.not_email=\u7528\u6237\u540d\u5fc5\u987b\u662f\u90ae\u7bb1
login.type=\u767b\u5f55
login.username=\u540d\u5b57
login.account=\u90ae\u7bb1
login.no_valid_character=\u65e0\u6709\u6548\u7684\u89d2\u8272\uff0c\u81ea\u884c\u5230skinme.cc\u767b\u9646\u5e76\u521b\u5efa\u89d2\u8272
login.failed.connect_authentication_server=\u65e0\u6cd5\u8fde\u63a5\u8ba4\u8bc1\u670d\u52a1\u5668,\u53ef\u80fd\u662f\u7f51\u7edc\u95ee\u9898
@@ -187,6 +188,8 @@ settings.fullscreen=\u5168\u5c4f
settings.update_version=\u66f4\u65b0\u7248\u672c\u6587\u4ef6
settings.physical_memory=\u7269\u7406\u5185\u5b58\u5927\u5c0f
settings.choose_javapath=\u9009\u62e9Java\u8def\u5f84
settings.default=\u9ed8\u8ba4
settings.custom=\u81ea\u5b9a\u4e49
settings.failed_load=\u8bbe\u7f6e\u6587\u4ef6\u52a0\u8f7d\u5931\u8d25\uff0c\u53ef\u80fd\u662f\u5347\u7ea7\u4e86\u542f\u52a8\u5668\u6216\u88ab\u4eba\u5de5\u4fee\u6539\u9020\u6210\u9519\u8bef\uff0c\u662f\u5426\u6e05\u9664\uff1f

View File

@@ -73,6 +73,7 @@ login.not_email=\u7528\u6236\u540d\u5fc5\u9808\u662f\u90f5\u7bb1
login.type=\u767b\u9304
login.username=\u540d\u5b57
login.account=\u90ae\u7bb1
login.no_valid_character=\u7121\u6709\u6548\u7684\u89d2\u8272\uff0c\u81ea\u884c\u5230skinme.cc\u767b\u9678\u4e26\u5275\u5efa\u89d2\u8272
login.failed.connect_authentication_server=\u7121\u6cd5\u9023\u63a5\u8a8d\u8b49\u670d\u52d9\u5668,\u53ef\u80fd\u662f\u7db2\u7d61\u554f\u984c
@@ -187,6 +188,8 @@ settings.fullscreen=\u5168\u5c4f
settings.update_version=\u66f4\u65b0\u7248\u672c\u6587\u4ef6
settings.physical_memory=\u7269\u7406\u5185\u5b58\u5927\u5c0f
settings.choose_javapath=\u9009\u62e9Java\u8def\u5f84
settings.default=\u9ed8\u8a8d
settings.custom=\u81ea\u5b9a\u7fa9
settings.failed_load=\u8a2d\u5b9a\u6587\u4ef6\u52a0\u8f09\u5931\u6557\uff0c\u53ef\u80fd\u662f\u5347\u7d1a\u4e86\u555f\u52d5\u5668\u6216\u88ab\u4eba\u5de5\u4fee\u6539\u9020\u6210\u932f\u8aa4\uff0c\u662f\u5426\u6e05\u9664\uff1f