-2

I've been trying to run a batch file (run.bat for a minecraft server) via Java console. while i did manage to figure out a way to run the batch script on java, it seems that i cannot send commands from the console. the image below is the problem, typing help will not execute a command, is there a way to this?

I'm not allowed to embed images yet so here:

Here's my code snippet:

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException{
        String line;
        Process p = Runtime.getRuntime().exec("run.bat");
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        while ((line = input.readLine()) != null) {
            System.out.println(line);
        }
        input.close();
    }
}
8
  • Try new ProcessBuilder("cmd.exe", "/c", "\\foo\\bar\\run.bat").inheritIO().start(); Commented Feb 22 at 13:21
  • 5
    By "send commands from the console", do you mean that you want to manually provide interactive input to the external program you're running via Java, in the console in which you are running Java? Commented Feb 22 at 13:34
  • 3
    Your question title is written as if you want to send code to a batch file, but your image and body text appears to be related to providing responses/interacting with something started from a batch file via some unspecified console. Please take time to Edit your question to better focus on the problem you need us to help you to resolve. Commented Feb 22 at 15:20
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Feb 22 at 16:09
  • In your setup is that you're only reading the output of the run.bat process. You're not connecting your Java console's input to the run.bat process's input. Commented Feb 23 at 2:45

1 Answer 1

0

You need to capture the process’s output in one thread and simultaneously send user input to the process in another.

One approach would be to start the batch with ProcessBuilder, redirect I/O, and use two threads (one reading p.getInputStream() and printing to System.out, the other reading System.in and writing to p.getOutputStream()).

ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", "run.bat");
pb.redirectErrorStream(true);
Process p = pb.start();

// Thread to read process output
new Thread(() -> {
    try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}).start();

// Main thread to send user commands
try (PrintWriter pw = new PrintWriter(p.getOutputStream(), true);
     BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in))) {
    String command;
    while ((command = userInput.readLine()) != null) {
        pw.println(command);
    }
}

Alternatively, if you only need to send commands programmatically (not interactively), consider enabling RCON in the server properties and sending commands via a Minecraft RCON library.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.