0

I am making a program to make a minecraft server. I structured the program as following:

  1. First it asks the user for the directory of the server jar file.

  2. Then it asks the user to enter the maximum and the minimum memory for the server.

  3. Then it creates a folder in the desktop and it moves the server jar file there.

  4. After that it creates a .bat file (without a pause statement at the end in order for it close right away) for the server to run.

  5. Then it runs this command: var firstProcess = runtime.exec("cmd /c run.bat", null, new File(folderPath));.

  6. Then it will wait until the eula.txt file can be read and can be writen:

while (!(new File(ePath)).exists()) {}
var eFile = new File(ePath);
while (!(eFile.canRead() && eFile.canWrite())) {}
  1. Then it scans the eula.txt file and adds its contents in a LinkedList. After it removes the last index of the LinkedList which is this: eula=false and replaces's it with this: eula=true.And then it rewrites the whole file:
var sc = new Scanner(new FileReader(ePath));
LinkedList elist = new LinkedList<>();

while(sc.hasNextLine())
    elist.add(sc.nextLine());

elist.removeLast();
elist.addLast("eula=true");

var bufferedWriter = new BufferedWriter(new FileWriter(ePath, false));

var eListToArray = elist.toArray();
for (int i = 0; i < eListToArray.length; i++)
    bufferedWriter.write(eListToArray[i] + "\n");

bufferedWriter.flush();
bufferedWriter.close();
  1. Then it deletes the .bat and it creates one with a pause statement at the end (because the user might change something at the server and produce an error in which case the pause statement will let the user see what caused the error instead of closing right away):
boolean d = serverFile.batFileWriter(ServerJarFile, "").delete();
if(d == true)
   System.out.println("Bat file successfully deleted!");

boolean c = serverFile.batFileWriter(ServerJarFile, "\npause").createNewFile();
if(c == true)
   System.out.println("Bat file successfully created!");

The whole code is this:

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Process {
    private Runtime runtime = Runtime.getRuntime();
    private String folderPath;
    private ServerFile serverFile;
    private File ServerJarFile;

    public Process(String folderPath, ServerFile serverFile, File serverJarFile) {
        this.folderPath = folderPath;
        this.serverFile = serverFile;
        ServerJarFile = serverJarFile;
    }

    public void process() throws IOException, InterruptedException {
        List<String> cmdOut = new ArrayList<>();
        String command = "cmd /c ";

        var firstProcess = runtime.exec(command + "run.bat", null, new File(folderPath));

        String ePath = folderPath + "\\eula.txt";
        while (!(new File(ePath)).exists()) {}
        var eFile = new File(ePath);
        while (!(eFile.canRead() && eFile.canWrite())) {}

        overwriteEFile(ePath);

        var secondProcess = runtime.exec(command + "run.bat", null, new File(folderPath));

        boolean d = serverFile.batFileWriter(ServerJarFile, "").delete();
        if(d == true)
            System.out.println("Bat file successfully deleted!");

        boolean c = serverFile.batFileWriter(ServerJarFile, "\npause").createNewFile();
        if(c == true)
            System.out.println("Bat file successfully created!");
    }

    private void endProcess(java.lang.Process process) throws IOException {
        var bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        while ((bufferedReader.readLine()) != null){
            System.out.println("sdfadf");
        }

        if (process.isAlive())
            process.destroy();
    }

    private void overwriteEFile(String ePath) throws IOException {
        var sc = new Scanner(new FileReader(ePath));
        LinkedList elist = new LinkedList<>();

        while(sc.hasNextLine())
            elist.add(sc.nextLine());

        elist.removeLast();

        elist.addLast("eula=true");


        var bufferedWriter = new BufferedWriter(new FileWriter(ePath, false));

        var eListToArray = elist.toArray();

        for (int i = 0; i < eListToArray.length; i++)
            bufferedWriter.write(eListToArray[i] + "\n");

        bufferedWriter.flush();
        bufferedWriter.close();
    }
}

The problem is that the second process running the .bat file (without the pause statements) won't close right away and when the program closes the cmd keeps running. If anyone knows how to fix this it would be greatly appreciated!!

user207421
  • 298,294
  • 41
  • 291
  • 462
John Ko.
  • 9
  • 3
  • Consider using [start](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/start) and maybe [javaw](https://stackoverflow.com/questions/1997718/difference-between-java-exe-and-javaw-exe) – Abra Jun 01 '21 at 19:17

1 Answers1

-1

Adding exit command at the end of your batch file will close the terminal.

If you don't want to put exit in your main batch for some reason, you can instead call it from another temporary batch like this:

call run.bat
exit

Java code:

Runtime.getRuntime.exec("cmd /c start /wait run.bat").waitFor();

example run.bat that I tested

timeout 5
exit

The start /wait will open a new terminal and exit will make sure to close it

Keray
  • 169
  • 3
  • I tried to put exit at the end but it doesn't close it. The bat file keeps running. – John Ko. Jun 02 '21 at 04:26
  • I also tried to kill the cmd from a command that i believe is like that ```runtime.exec("taskkill /f /im cmd.exe")``` – John Ko. Jun 02 '21 at 04:28
  • A command interpreter will automatically exit at the end of the batch file it was invoked to execute. – user207421 Jun 02 '21 at 04:36
  • I need a way to force close cmd after I know it run after the `eula.txt` modification because from the `run.bat` file you run the server and you can type commands related to the server and as long this is open the server stays opened. – John Ko. Jun 02 '21 at 10:32
  • I edited my answer with an example that I tested and made sure it works – Keray Jun 07 '21 at 16:46