bat文件在java中完成其工作后,如何关闭cmd?

bat文件在java中完成其工作后,如何关闭cmd?,java,Java,我正在制作一个程序来制作minecraft服务器。我按照以下方式组织了该计划: 首先,它要求用户提供服务器jar文件的目录 然后它要求用户输入服务器的最大和最小内存 然后它在桌面上创建一个文件夹,并将服务器jar文件移动到那里 之后,它会创建一个.bat文件,结尾不带pause语句,以便立即关闭该文件,以便服务器运行 然后运行以下命令: var firstProcess=runtime.execcmd/c run.bat,null,新文件folderpath 然后,它将等待直到可以读取和写入eu

我正在制作一个程序来制作minecraft服务器。我按照以下方式组织了该计划:

首先,它要求用户提供服务器jar文件的目录

然后它要求用户输入服务器的最大和最小内存

然后它在桌面上创建一个文件夹,并将服务器jar文件移动到那里

之后,它会创建一个.bat文件,结尾不带pause语句,以便立即关闭该文件,以便服务器运行

然后运行以下命令: var firstProcess=runtime.execcmd/c run.bat,null,新文件folderpath

然后,它将等待直到可以读取和写入eula.txt文件:

然后扫描eula.txt文件并将其内容添加到LinkedList中。删除LinkedList的最后一个索引:eula=false并将其替换为:eula=true。然后重写整个文件: 整个代码如下:

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();
    }
}
问题是运行.bat文件的第二个进程如果没有pause语句,将无法立即关闭,并且当程序关闭时,cmd将继续运行。如果有人知道如何解决这个问题,我们将不胜感激

在批处理文件末尾添加exit命令将关闭终端

如果出于某种原因不想在主批处理中放入exit,可以从另一个临时批处理调用它,如下所示:

call run.bat
exit
Java代码:

Runtime.getRuntime.exec("cmd /c start /wait run.bat").waitFor();
我测试的run.bat示例

timeout 5
exit

开始/等待将打开一个新的终端,退出将确保关闭它

考虑使用,也许我试图将退出放在末尾,但它没有关闭它。bat文件一直在运行。我还尝试从一个命令中终止cmd,我相信这类似于runtime.exectaskkill/f/im cmd.exeA命令解释器将在调用它以执行的批处理文件结束时自动退出。我需要一种方法在知道它在eula.txt修改后运行后强制关闭cmd,因为它是从run.bat文件中运行的您运行服务器,可以键入与服务器相关的命令,只要服务器处于打开状态,服务器就会保持打开状态。我用一个我测试过的示例编辑了我的答案,并确保它正常工作
call run.bat
exit
Runtime.getRuntime.exec("cmd /c start /wait run.bat").waitFor();
timeout 5
exit