Client client = new Client(conf); int exitCode = client.dispatchToServer();
System.exit(exitCode);
}
privateint dispatchToServer() { try { // Check if server seems to be already running if (!conf.portFile().hasValidValues()) { // Fork a new server and wait for it to start
startNewServer();
}
try (Socket socket = tryConnect()) {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
Protocol.sendCommand(out, conf.javacArgs()); int exitCode = Protocol.readResponse(in);
while (true) {
Log.debug("Trying to connect. Attempt " + (++attempt) + " of " + MAX_CONNECT_ATTEMPTS); try {
Socket socket = new Socket();
InetAddress localhost = InetAddress.getByName(null);
InetSocketAddress address = new InetSocketAddress(localhost, conf.portFile().getPort());
socket.connect(address, CONNECTION_TIMEOUT);
Log.debug("Connected"); return socket;
} catch (IOException ex) {
Log.error("Connection attempt failed: " + ex.getMessage()); if (attempt >= MAX_CONNECT_ATTEMPTS) {
Log.error("Giving up"); thrownew IOException("Could not connect to server", ex);
}
} Thread.sleep(WAIT_BETWEEN_CONNECT_ATTEMPTS);
}
}
/* *Forkaserverprocessandwaitforservertocomearound
*/ privatevoid startNewServer() throws IOException, InterruptedException {
List<String> cmd = new ArrayList<>(); // conf.javaCommand() is how to start java in the way we want to run // the server
cmd.addAll(Arrays.asList(conf.javaCommand().split(" "))); // javacserver.server.Server is the server main class
cmd.add(Server.class.getName()); // and it expects a port file path
cmd.add(conf.portFile().getFilename());
Process serverProcess;
Log.debug("Starting server. Command: " + String.join(" ", cmd)); try { // If the cmd for some reason can't be executed (file is not found, // or is not executable for instance) this will throw an // IOException
serverProcess = new ProcessBuilder(cmd).redirectErrorStream(true).start();
} catch (IOException ex) { // Message is typically something like: // Cannot run program "xyz": error=2, No such file or directory
Log.error("Failed to create server process: " + ex.getMessage());
Log.debug(ex); thrownew IOException(ex);
}
// serverProcess != null at this point. try { // Throws an IOException if no valid values materialize
conf.portFile().waitForValidValues();
} catch (IOException ex) { // Process was started, but server failed to initialize. This could // for instance be due to the JVM not finding the server class, // or the server running in to some exception early on.
Log.error("javacserver server process failed to initialize: " + ex.getMessage());
Log.error("Process output:");
Reader serverStdoutStderr = new InputStreamReader(serverProcess.getInputStream()); try (BufferedReader br = new BufferedReader(serverStdoutStderr)) {
br.lines().forEach(Log::error);
}
Log.error("<End of process output>"); try {
Log.error("Process exit code: " + serverProcess.exitValue());
} catch (IllegalThreadStateException e) { // Server is presumably still running.
} thrownew IOException("Server failed to initialize: " + ex.getMessage(), ex);
}
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.9 Sekunden
(vorverarbeitet am 2026-06-10)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.