[JAVA] Send a command to Spigot from an external process

I had to send a command to Spigot's console for interprocess communication, so I'll leave that method.

Server.java


public class Server {

    private Process process = null;
    private BufferedWriter commandLine;
    
    public void boot() {
        ProcessBuilder pb = new ProcessBuilder("java","-jar","~.jar");
        pb.directory(folder);

        try {
            process = pb.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        commandLine = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
    }

    public void sendCommand(String command) {
        try {
            commandLine.write(command + "\n");
            commandLine.flush();
        } catch (IOException e){
            e.printStackTrace();
        }
    }

    public void stop() {
        if (process == null) return;
        sendCommand("stop");
        try {
            commandLine.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

A fairly suitable code

Commands can be sent by writing to the process's OutputStream using BufferedWriter. This is starting the server externally, but it should be possible for processes that are already running ~~ Note that it will not be executed unless a line break occurs

Recommended Posts

Send a command to Spigot from an external process
Load an external jar from a Spring Boot fat jar
File path is not recognized when executing an external command from a Java application
[iOS] [Objective-C] How to update a widget from an Objective-C app
Send a pull request to GitHub
How to insert an external library
How to run a Kotlin Coroutine sample from the command line
[Swift] How to send a notification
When you receive a call, send an SMS to that number
Run an external process in Java
Procedure for connecting from a virtual machine to an external network via a proxy server (VirtualBox, Windows10, CentOS7)
Why assign an ArrayList to a List
Send an email from gmail with Ruby
A program that calculates factorials from 2 to 100
Java: How to send values from Servlet to Servlet
[swift5] How to transition from the app to an external site by specifying the URL
How to start a subscript from an arbitrary number in Ruby iterative processing
From creating a Spring Boot project to running an application with VS Code
A memorandum that the author suffered from an error [bundler: failed to load command: unicorn NameError: uninitialized constant Model]
Transform from a normal class to a lambda expression
I want to send an email in Java.
Connect to Aurora (MySQL) from a Java application
How to get a heapdump from a Docker container
To become a VB.net programmer from a Java shop
How to delete a controller etc. using a command
[Java] Get a random value from an array
From building an AWS cloud environment to deploying a Spring Boot app (for beginners)