Adds working test code for writing and reading console

This commit is contained in:
Kristian Knarvik 2018-01-26 15:59:49 +01:00
parent b9963c3a89
commit 83f012cf7f
2 changed files with 31 additions and 10 deletions

View File

@ -29,7 +29,7 @@ public class Server {
private ServerType type; private ServerType type;
private String serverVersion; private String serverVersion;
private String maxRam; private String maxRam;
private long pid; private Process process;
public Server(String name) { public Server(String name) {
this.name = name; this.name = name;
@ -39,7 +39,7 @@ public class Server {
this.type = null; this.type = null;
this.serverVersion = null; this.serverVersion = null;
this.maxRam = ramList[0]; this.maxRam = ramList[0];
this.pid = -1; this.process = null;
servers.add(this); servers.add(this);
} }
@ -64,6 +64,10 @@ public class Server {
return servers; return servers;
} }
public Process getProcess() {
return this.process;
}
public void toggle() { public void toggle() {
this.enabled = !this.enabled; this.enabled = !this.enabled;
} }
@ -120,11 +124,13 @@ public class Server {
System.out.println("File was not found."); System.out.println("File was not found.");
return; return;
} }
Runtime rt = Runtime.getRuntime(); //Runtime rt = Runtime.getRuntime();
try { try {
System.out.println("Executing command."); ProcessBuilder builder = new ProcessBuilder("java", "-Xmx" + this.maxRam, "-Xms512M", "-Djline.terminal=jline.UnsupportedTerminal", "-Dcom.mojang.eula.agree=true", "-jar", "\"" + this.path + "\\" + this.getType() + "\"");
Process pr = rt.exec("\"java\" -Xmx" + this.maxRam + " -Xms512M -jar " + "\"" + this.path + "\\" + this.type + "\" nogui", null, new File(this.path)); builder.directory(new File(this.path));
this.pid = pr.pid(); builder.redirectErrorStream(true);
Process pr = builder.start();
this.process = pr;
System.out.println("Success"); System.out.println("Success");
} catch (IOException e) { } catch (IOException e) {
System.out.println("Error"); System.out.println("Error");

View File

@ -1,7 +1,8 @@
import javax.naming.ConfigurationException; import javax.naming.ConfigurationException;
import net.knarcraft.serverlauncher.server.*; import net.knarcraft.serverlauncher.server.*;
import java.util.ArrayList;
import java.io.*;
/** /**
* A class for testing new and existing features. * A class for testing new and existing features.
@ -11,7 +12,7 @@ import java.util.ArrayList;
* @since 0.0.0.1 * @since 0.0.0.1
*/ */
public class ServerTest { public class ServerTest {
public static void main(String[] args) { public static void main(String[] args) throws IOException {
try { try {
ServerType.loadServerTypes(); ServerType.loadServerTypes();
} catch (ConfigurationException e) { } catch (ConfigurationException e) {
@ -22,9 +23,23 @@ public class ServerTest {
Server server1 = Server.getServers().get(0); Server server1 = Server.getServers().get(0);
server1.toggle(); server1.toggle();
server1.setPath("C:\\Users\\Kristian\\Desktop\\Test"); server1.setPath("C:\\Users\\Kristian\\Desktop\\Test");
server1.setType(ServerType.getServerTypes().get(6)); server1.setType(ServerType.getServerTypes().get(4));
server1.setServerVersion("1.6.4"); server1.setServerVersion("1.12.2");
server1.setMaxRam("1G"); server1.setMaxRam("1G");
Server.startServers(); 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();
} }
} }