From 64af70e44a2dec6b303a509421b34b852bf13681 Mon Sep 17 00:00:00 2001 From: sirjonasxx <36828922+sirjonasxx@users.noreply.github.com> Date: Sat, 1 Dec 2018 13:49:29 +0100 Subject: [PATCH] potential fix executing extensions X-platform --- .../ui/extensions/executer/ExecutionInfo.java | 22 ++++++++++++------- .../executer/NormalExtensionRunner.java | 22 +++++++++++-------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/G-Earth/src/main/java/gearth/ui/extensions/executer/ExecutionInfo.java b/G-Earth/src/main/java/gearth/ui/extensions/executer/ExecutionInfo.java index 428a82a..d13203d 100644 --- a/G-Earth/src/main/java/gearth/ui/extensions/executer/ExecutionInfo.java +++ b/G-Earth/src/main/java/gearth/ui/extensions/executer/ExecutionInfo.java @@ -10,29 +10,35 @@ import java.util.Map; */ public class ExecutionInfo { - private static Map extensionTypeToExecutionCommand; + private static Map extensionTypeToExecutionCommand; public final static List ALLOWEDEXTENSIONTYPES; public final static String EXTENSIONSDIRECTORY = "Extensions"; static { extensionTypeToExecutionCommand = new HashMap<>(); - extensionTypeToExecutionCommand.put("*.jar","java -jar \"{path}\""); - extensionTypeToExecutionCommand.put("*.py","python \"{path}\""); - extensionTypeToExecutionCommand.put("*.py3","python3 \"{path}\""); - extensionTypeToExecutionCommand.put("*.sh","\"{path}\""); - extensionTypeToExecutionCommand.put("*.exe","\"{path}\""); + extensionTypeToExecutionCommand.put("*.jar", new String[]{"java", "-jar", "{path}"}); + extensionTypeToExecutionCommand.put("*.py", new String[]{"python", "{path}"}); + extensionTypeToExecutionCommand.put("*.py3", new String[]{"python3", "{path}"}); + extensionTypeToExecutionCommand.put("*.sh", new String[]{"{path}"}); + extensionTypeToExecutionCommand.put("*.exe", new String[]{"{path}"}); + String[] extraArgs = {"-p", "{port}", "-f", "{filename}", "-c", "{cookie}"}; for(String type : extensionTypeToExecutionCommand.keySet()) { + String[] commandShort = extensionTypeToExecutionCommand.get(type); + String[] combined = new String[extraArgs.length + commandShort.length]; + System.arraycopy(commandShort, 0, combined, 0, commandShort.length); + System.arraycopy(extraArgs, 0, combined, commandShort.length, extraArgs.length); + extensionTypeToExecutionCommand.put( type, - extensionTypeToExecutionCommand.get(type) + " -p {port} -f \"{filename}\" -c {cookie}" + combined ); } ALLOWEDEXTENSIONTYPES = new ArrayList<>(extensionTypeToExecutionCommand.keySet()); } - public static String getExecutionCommand(String type) { + public static String[] getExecutionCommand(String type) { return extensionTypeToExecutionCommand.get(type); } diff --git a/G-Earth/src/main/java/gearth/ui/extensions/executer/NormalExtensionRunner.java b/G-Earth/src/main/java/gearth/ui/extensions/executer/NormalExtensionRunner.java index 6ee4a87..e738245 100644 --- a/G-Earth/src/main/java/gearth/ui/extensions/executer/NormalExtensionRunner.java +++ b/G-Earth/src/main/java/gearth/ui/extensions/executer/NormalExtensionRunner.java @@ -86,15 +86,19 @@ public class NormalExtensionRunner implements ExtensionRunner { public void tryRunExtension(String path, int port) { try { String filename = Paths.get(path).getFileName().toString(); - String execCommand = ExecutionInfo.getExecutionCommand(getFileExtension(path)) - .replace("{path}", path) - .replace("{port}", port+"") - .replace("{filename}", filename) - .replace("{cookie}", Authenticator.generateCookieForExtension(filename)); - Process proc = Runtime.getRuntime().exec(execCommand); - - - + String[] execCommand = ExecutionInfo.getExecutionCommand(getFileExtension(path)); + execCommand = Arrays.copyOf(execCommand, execCommand.length); + String cookie = Authenticator.generateCookieForExtension(filename); + for (int i = 0; i < execCommand.length; i++) { + execCommand[i] = execCommand[i] + .replace("{path}", path) + .replace("{port}", port+"") + .replace("{filename}", filename) + .replace("{cookie}", cookie); + } + ProcessBuilder pb = new ProcessBuilder(execCommand); +// Process proc = Runtime.getRuntime().exec(execCommand); + Process proc = pb.start(); if (Main.hasFlag(ExtensionRunner.SHOW_EXTENSIONS_LOG)) { String sep = "" + System.lineSeparator();