supported linux memory reading
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher.
|
||||
* Copyright (C) 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 3 of the License, or
|
||||
@@ -40,6 +40,7 @@ public class TaskList extends Thread {
|
||||
boolean shouldContinue = true;
|
||||
|
||||
public TaskList() {
|
||||
setDaemon(true);
|
||||
}
|
||||
|
||||
public void clean() {
|
||||
@@ -71,6 +72,7 @@ public class TaskList extends Thread {
|
||||
public InvokeThread(Task task, Set<InvokeThread> ss) {
|
||||
this.task = task;
|
||||
s = ss;
|
||||
setDaemon(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -18,14 +18,12 @@
|
||||
package org.jackhuang.hellominecraft.utils;
|
||||
|
||||
import com.sun.management.OperatingSystemMXBean;
|
||||
import java.awt.Desktop;
|
||||
import java.awt.Image;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.datatransfer.StringSelection;
|
||||
import java.io.File;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
@@ -33,7 +31,6 @@ import java.net.URLClassLoader;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.Random;
|
||||
import javax.swing.ImageIcon;
|
||||
import org.jackhuang.hellominecraft.C;
|
||||
import org.jackhuang.hellominecraft.HMCLog;
|
||||
|
||||
/**
|
||||
@@ -79,16 +76,6 @@ public final class Utils {
|
||||
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(text), null);
|
||||
}
|
||||
|
||||
public static void openFolder(File f) {
|
||||
try {
|
||||
f.mkdirs();
|
||||
java.awt.Desktop.getDesktop().open(f);
|
||||
} catch (Exception ex) {
|
||||
MessageBox.Show(C.i18n("message.cannot_open_explorer") + ex.getMessage());
|
||||
HMCLog.warn("Failed to open folder:" + f, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static ImageIcon scaleImage(ImageIcon i, int x, int y) {
|
||||
return new ImageIcon(i.getImage().getScaledInstance(x, y, Image.SCALE_SMOOTH));
|
||||
}
|
||||
@@ -153,16 +140,11 @@ public final class Utils {
|
||||
*
|
||||
* @param status exit code
|
||||
*/
|
||||
public static void shutdownForcely(int status) {
|
||||
try {
|
||||
Class z = Class.forName("java.lang.Shutdown");
|
||||
Method exit = z.getDeclaredMethod("exit", int.class);
|
||||
exit.setAccessible(true);
|
||||
exit.invoke(z, status);
|
||||
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
||||
MessageBox.Show(C.i18n("launcher.exit_failed"));
|
||||
e.printStackTrace();
|
||||
}
|
||||
public static void shutdownForcely(int status) throws Exception {
|
||||
Class z = Class.forName("java.lang.Shutdown");
|
||||
Method exit = z.getDeclaredMethod("exit", int.class);
|
||||
exit.setAccessible(true);
|
||||
exit.invoke(z, status);
|
||||
}
|
||||
|
||||
public static void requireNonNull(Object o) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher.
|
||||
* Copyright (C) 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 3 of the License, or
|
||||
@@ -17,10 +17,8 @@
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.utils.system;
|
||||
|
||||
import org.jackhuang.hellominecraft.utils.MessageBox;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import org.jackhuang.hellominecraft.C;
|
||||
import org.jackhuang.hellominecraft.utils.CollectionUtils;
|
||||
import org.jackhuang.hellominecraft.utils.Event;
|
||||
import org.jackhuang.hellominecraft.utils.EventHandler;
|
||||
@@ -33,7 +31,20 @@ import org.jackhuang.hellominecraft.utils.StrUtils;
|
||||
public class JavaProcessMonitor {
|
||||
|
||||
private final HashSet<Thread> al = new HashSet<>();
|
||||
/**
|
||||
* this event will be executed only if the application returned 0.
|
||||
*/
|
||||
public final EventHandler<JavaProcess> stoppedEvent = new EventHandler<>(this);
|
||||
/**
|
||||
* When the monitored application exited with exit code not zero, this event
|
||||
* will be executed. Event args is the exit code.
|
||||
*/
|
||||
public final EventHandler<Integer> applicationExitedAbnormallyEvent = new EventHandler<>(this);
|
||||
/**
|
||||
* When jvm crashed, this event will be executed. Event args is the exit
|
||||
* code.
|
||||
*/
|
||||
public final EventHandler<Integer> jvmLaunchFailedEvent = new EventHandler<>(this);
|
||||
private final JavaProcess p;
|
||||
|
||||
public JavaProcessMonitor(JavaProcess p) {
|
||||
@@ -43,7 +54,7 @@ public class JavaProcessMonitor {
|
||||
public void start() {
|
||||
Event<JavaProcess> event = (sender2, t) -> {
|
||||
if (t.getExitCode() != 0)
|
||||
MessageBox.Show(C.i18n("launch.exited_abnormally"));
|
||||
applicationExitedAbnormallyEvent.execute(t.getExitCode());
|
||||
processThreadStopped((ProcessThread) sender2, false);
|
||||
return true;
|
||||
};
|
||||
@@ -51,7 +62,7 @@ public class JavaProcessMonitor {
|
||||
if (p1.getExitCode() != 0 && p1.getStdErrLines().size() > 0 && StrUtils.containsOne(p1.getStdErrLines(), Arrays.asList("Could not create the Java Virtual Machine.",
|
||||
"Error occurred during initialization of VM",
|
||||
"A fatal exception has occurred. Program will exit.")))
|
||||
MessageBox.Show(C.i18n("launch.cannot_create_jvm"));
|
||||
jvmLaunchFailedEvent.execute(p1.getExitCode());
|
||||
processThreadStopped((ProcessThread) sender3, false);
|
||||
return true;
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher.
|
||||
* Copyright (C) 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 3 of the License, or
|
||||
@@ -18,7 +18,13 @@
|
||||
package org.jackhuang.hellominecraft.utils.system;
|
||||
|
||||
import com.sun.management.OperatingSystemMXBean;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.util.StringTokenizer;
|
||||
import org.jackhuang.hellominecraft.HMCLog;
|
||||
|
||||
/**
|
||||
@@ -40,9 +46,8 @@ public enum OS {
|
||||
}
|
||||
|
||||
public static OS os() {
|
||||
String str;
|
||||
if ((str = System.getProperty("os.name").toLowerCase())
|
||||
.contains("win"))
|
||||
String str = System.getProperty("os.name").toLowerCase();
|
||||
if (str.contains("win"))
|
||||
return OS.WINDOWS;
|
||||
if (str.contains("mac"))
|
||||
return OS.OSX;
|
||||
@@ -62,12 +67,45 @@ public enum OS {
|
||||
*/
|
||||
public static long getTotalPhysicalMemory() {
|
||||
try {
|
||||
OperatingSystemMXBean o = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
|
||||
return o.getTotalPhysicalMemorySize();
|
||||
if (os() == LINUX)
|
||||
return memoryInfoForLinux()[0] * 1024;
|
||||
else {
|
||||
OperatingSystemMXBean o = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
|
||||
return o.getTotalPhysicalMemorySize();
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
HMCLog.warn("Failed to get total physical memory size");
|
||||
HMCLog.warn("Failed to get total physical memory size", t);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public static long[] memoryInfoForLinux() throws IOException {
|
||||
File file = new File("/proc/meminfo");
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(
|
||||
new FileInputStream(file)));
|
||||
long[] result = new long[4];
|
||||
String str = null;
|
||||
StringTokenizer token;
|
||||
while ((str = br.readLine()) != null) {
|
||||
token = new StringTokenizer(str);
|
||||
if (!token.hasMoreTokens())
|
||||
continue;
|
||||
|
||||
str = token.nextToken();
|
||||
if (!token.hasMoreTokens())
|
||||
continue;
|
||||
|
||||
if (str.equalsIgnoreCase("MemTotal:"))
|
||||
result[0] = Long.parseLong(token.nextToken());
|
||||
else if (str.equalsIgnoreCase("MemFree:"))
|
||||
result[1] = Long.parseLong(token.nextToken());
|
||||
else if (str.equalsIgnoreCase("SwapTotal:"))
|
||||
result[2] = Long.parseLong(token.nextToken());
|
||||
else if (str.equalsIgnoreCase("SwapFree:"))
|
||||
result[3] = Long.parseLong(token.nextToken());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.jackhuang.hellominecraft.logging.Level;
|
||||
import org.jackhuang.hellominecraft.utils.functions.NonFunction;
|
||||
import org.jackhuang.hellominecraft.utils.DoubleOutputStream;
|
||||
import org.jackhuang.hellominecraft.utils.LauncherPrintStream;
|
||||
import org.jackhuang.hellominecraft.utils.MessageBox;
|
||||
import org.jackhuang.hellominecraft.utils.Utils;
|
||||
|
||||
/**
|
||||
@@ -208,7 +209,12 @@ public class LogWindow extends javax.swing.JFrame {
|
||||
if (flag)
|
||||
this.dispose();
|
||||
else
|
||||
Utils.shutdownForcely(0);
|
||||
try {
|
||||
Utils.shutdownForcely(0);
|
||||
} catch (Exception e) {
|
||||
MessageBox.Show(C.i18n("launcher.exit_failed"));
|
||||
HMCLog.err("Failed to shutdown forcely", e);
|
||||
}
|
||||
}//GEN-LAST:event_btnCloseActionPerformed
|
||||
|
||||
private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
|
||||
|
||||
@@ -19,6 +19,8 @@ package org.jackhuang.hellominecraft.views;
|
||||
|
||||
import java.awt.EventQueue;
|
||||
import java.awt.FontMetrics;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import javax.swing.DefaultListModel;
|
||||
import javax.swing.JLabel;
|
||||
@@ -28,9 +30,12 @@ import javax.swing.JScrollPane;
|
||||
import javax.swing.JTable;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
import org.jackhuang.hellominecraft.C;
|
||||
import org.jackhuang.hellominecraft.HMCLog;
|
||||
import org.jackhuang.hellominecraft.utils.MessageBox;
|
||||
import org.jackhuang.hellominecraft.utils.StrUtils;
|
||||
import org.jackhuang.hellominecraft.utils.functions.NonFunction;
|
||||
import org.jackhuang.hellominecraft.utils.system.OS;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -50,8 +55,8 @@ public class SwingUtils {
|
||||
*/
|
||||
public static DefaultTableModel makeDefaultTableModel(String[] titleA, final Class[] typesA, final boolean[] canEditA) {
|
||||
return new DefaultTableModel(
|
||||
new Object[][]{},
|
||||
titleA) {
|
||||
new Object[][]{},
|
||||
titleA) {
|
||||
Class[] types = typesA;
|
||||
boolean[] canEdit = canEditA;
|
||||
|
||||
@@ -67,6 +72,28 @@ public class SwingUtils {
|
||||
};
|
||||
}
|
||||
|
||||
public static void openFolder(File f) {
|
||||
f.mkdirs();
|
||||
String path = f.getAbsolutePath();
|
||||
switch (OS.os()) {
|
||||
case OSX:
|
||||
try {
|
||||
Runtime.getRuntime().exec(new String[]{"/usr/bin/open", path});
|
||||
} catch (IOException ex) {
|
||||
HMCLog.err("Failed to open " + path + " through /usr/bin/open", ex);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
try {
|
||||
java.awt.Desktop.getDesktop().open(f);
|
||||
} catch (Throwable ex) {
|
||||
MessageBox.Show(C.i18n("message.cannot_open_explorer") + ex.getMessage());
|
||||
HMCLog.warn("Failed to open " + path + " through java.awt.Desktop.getDesktop().open()", ex);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Open URL by java.awt.Desktop
|
||||
*
|
||||
@@ -76,6 +103,12 @@ public class SwingUtils {
|
||||
try {
|
||||
java.awt.Desktop.getDesktop().browse(new URI(link));
|
||||
} catch (Throwable e) {
|
||||
if (OS.os() == OS.OSX)
|
||||
try {
|
||||
Runtime.getRuntime().exec(new String[]{"/usr/bin/open", link});
|
||||
} catch (IOException ex) {
|
||||
HMCLog.warn("Failed to open link: " + link, ex);
|
||||
}
|
||||
HMCLog.warn("Failed to open link: " + link, e);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user