mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-10 04:16:02 -04:00
优化 FXUtils.testLinuxCommand (#3315)
This commit is contained in:
parent
4c1e607b8e
commit
9196bda537
@ -84,6 +84,7 @@ import java.net.URI;
|
|||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
@ -411,15 +412,20 @@ public final class FXUtils {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean testLinuxCommand(String command) {
|
private static String which(String command) {
|
||||||
try (final InputStream is = Runtime.getRuntime().exec(new String[]{"which", command}).getInputStream()) {
|
String path = System.getenv("PATH");
|
||||||
if (is.read() != -1) {
|
if (path == null)
|
||||||
return true;
|
return null;
|
||||||
}
|
|
||||||
|
for (String item : path.split(OperatingSystem.PATH_SEPARATOR)) {
|
||||||
|
try {
|
||||||
|
Path program = Paths.get(item, command);
|
||||||
|
if (Files.isExecutable(program))
|
||||||
|
return program.toRealPath().toString();
|
||||||
} catch (Throwable ignored) {
|
} catch (Throwable ignored) {
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void showFileInExplorer(Path file) {
|
public static void showFileInExplorer(Path file) {
|
||||||
@ -430,7 +436,7 @@ public final class FXUtils {
|
|||||||
openCommands = new String[]{"explorer.exe", "/select,", path};
|
openCommands = new String[]{"explorer.exe", "/select,", path};
|
||||||
else if (OperatingSystem.CURRENT_OS == OperatingSystem.OSX)
|
else if (OperatingSystem.CURRENT_OS == OperatingSystem.OSX)
|
||||||
openCommands = new String[]{"/usr/bin/open", "-R", path};
|
openCommands = new String[]{"/usr/bin/open", "-R", path};
|
||||||
else if (OperatingSystem.CURRENT_OS.isLinuxOrBSD() && testLinuxCommand("dbus-send"))
|
else if (OperatingSystem.CURRENT_OS.isLinuxOrBSD() && which("dbus-send") != null)
|
||||||
openCommands = new String[]{
|
openCommands = new String[]{
|
||||||
"dbus-send",
|
"dbus-send",
|
||||||
"--print-reply",
|
"--print-reply",
|
||||||
@ -496,13 +502,14 @@ public final class FXUtils {
|
|||||||
}
|
}
|
||||||
if (OperatingSystem.CURRENT_OS.isLinuxOrBSD()) {
|
if (OperatingSystem.CURRENT_OS.isLinuxOrBSD()) {
|
||||||
for (String browser : linuxBrowsers) {
|
for (String browser : linuxBrowsers) {
|
||||||
try (final InputStream is = Runtime.getRuntime().exec(new String[]{"which", browser}).getInputStream()) {
|
String path = which(browser);
|
||||||
if (is.read() != -1) {
|
if (path != null) {
|
||||||
|
try {
|
||||||
Runtime.getRuntime().exec(new String[]{browser, link});
|
Runtime.getRuntime().exec(new String[]{browser, link});
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
} catch (Throwable ignored) {
|
} catch (Throwable ignored) {
|
||||||
}
|
}
|
||||||
|
}
|
||||||
LOG.warning("No known browser found");
|
LOG.warning("No known browser found");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user