I am making a program to make a minecraft server. I structured the program as following:
First it asks the user for the directory of the server jar file.
Then it asks the user to enter the maximum and the minimum memory for the server.
Then it creates a folder in the desktop and it moves the server jar file there.
After that it creates a
.batfile (without apausestatement at the end in order for it close right away) for the server to run.Then it runs this command:
var firstProcess = runtime.exec("cmd /c run.bat", null, new File(folderPath));.Then it will wait until the
eula.txtfile can be read and can be writen:
while (!(new File(ePath)).exists()) {}
var eFile = new File(ePath);
while (!(eFile.canRead() && eFile.canWrite())) {}
- Then it scans the
eula.txtfile and adds its contents in aLinkedList. After it removes the last index of theLinkedListwhich is this:eula=falseand 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();
- Then it deletes the
.batand 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 thepausestatement 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!!