From 83f012cf7fd3b19956a2c10ac890432bf58ae3a2 Mon Sep 17 00:00:00 2001 From: Kristian Knarvik Date: Fri, 26 Jan 2018 15:59:49 +0100 Subject: [PATCH] Adds working test code for writing and reading console --- .../serverlauncher/server/Server.java | 18 ++++++++++----- test/ServerTest.java | 23 +++++++++++++++---- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/net/knarcraft/serverlauncher/server/Server.java b/src/net/knarcraft/serverlauncher/server/Server.java index 8be4c8f..977342f 100644 --- a/src/net/knarcraft/serverlauncher/server/Server.java +++ b/src/net/knarcraft/serverlauncher/server/Server.java @@ -29,7 +29,7 @@ public class Server { private ServerType type; private String serverVersion; private String maxRam; - private long pid; + private Process process; public Server(String name) { this.name = name; @@ -39,7 +39,7 @@ public class Server { this.type = null; this.serverVersion = null; this.maxRam = ramList[0]; - this.pid = -1; + this.process = null; servers.add(this); } @@ -64,6 +64,10 @@ public class Server { return servers; } + public Process getProcess() { + return this.process; + } + public void toggle() { this.enabled = !this.enabled; } @@ -120,11 +124,13 @@ public class Server { System.out.println("File was not found."); return; } - Runtime rt = Runtime.getRuntime(); + //Runtime rt = Runtime.getRuntime(); try { - System.out.println("Executing command."); - Process pr = rt.exec("\"java\" -Xmx" + this.maxRam + " -Xms512M -jar " + "\"" + this.path + "\\" + this.type + "\" nogui", null, new File(this.path)); - this.pid = pr.pid(); + ProcessBuilder builder = new ProcessBuilder("java", "-Xmx" + this.maxRam, "-Xms512M", "-Djline.terminal=jline.UnsupportedTerminal", "-Dcom.mojang.eula.agree=true", "-jar", "\"" + this.path + "\\" + this.getType() + "\""); + builder.directory(new File(this.path)); + builder.redirectErrorStream(true); + Process pr = builder.start(); + this.process = pr; System.out.println("Success"); } catch (IOException e) { System.out.println("Error"); diff --git a/test/ServerTest.java b/test/ServerTest.java index 2004644..8e6fd38 100644 --- a/test/ServerTest.java +++ b/test/ServerTest.java @@ -1,7 +1,8 @@ import javax.naming.ConfigurationException; import net.knarcraft.serverlauncher.server.*; -import java.util.ArrayList; + +import java.io.*; /** * A class for testing new and existing features. @@ -11,7 +12,7 @@ import java.util.ArrayList; * @since 0.0.0.1 */ public class ServerTest { - public static void main(String[] args) { + public static void main(String[] args) throws IOException { try { ServerType.loadServerTypes(); } catch (ConfigurationException e) { @@ -22,9 +23,23 @@ public class ServerTest { Server server1 = Server.getServers().get(0); server1.toggle(); server1.setPath("C:\\Users\\Kristian\\Desktop\\Test"); - server1.setType(ServerType.getServerTypes().get(6)); - server1.setServerVersion("1.6.4"); + server1.setType(ServerType.getServerTypes().get(4)); + server1.setServerVersion("1.12.2"); server1.setMaxRam("1G"); Server.startServers(); + + OutputStream stdin = server1.getProcess().getOutputStream (); + InputStream stdout = server1.getProcess().getInputStream (); + + BufferedReader reader = new BufferedReader (new InputStreamReader(stdout)); + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin)); + + String line; + while ((line = reader.readLine ()) != null) { + System.out.println ("Stdout: " + line); + writer.write("stop\n"); + writer.flush(); + } + //writer.close(); } }