From c721aef2a6c5368b70e1983029782f10e0bef0fd Mon Sep 17 00:00:00 2001 From: Kristian Knarvik Date: Mon, 17 Aug 2020 11:26:45 +0200 Subject: [PATCH] Tries a new fix to remove the pesky FileNotFoundException caused by the server version container --- .../server/ServerVersionContainer.java | 5 ++++ .../utility/CommonFunctions.java | 28 +++++++++++++++++++ .../server/ServerVersionContainerTest.java | 9 +++++- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainer.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainer.java index d0ed61f..a95680a 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainer.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainer.java @@ -92,6 +92,11 @@ public class ServerVersionContainer implements java.io.Serializable { PrintWriter file; try { if (!saveFile.exists()) { + if (!saveFile.getParentFile().exists()) { + if (!saveFile.getParentFile().mkdirs()) { + throw new FileNotFoundException("Unable to create folder for version file"); + } + } if (!saveFile.createNewFile()) { throw new FileNotFoundException("Unable to create version file"); } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java index adab827..ff8dc93 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java @@ -1,5 +1,6 @@ package net.knarcraft.minecraftserverlauncher.utility; +import net.knarcraft.minecraftserverlauncher.Main; import net.knarcraft.minecraftserverlauncher.userinterface.ServerLauncherGUI; import java.io.*; @@ -20,6 +21,33 @@ import java.util.Scanner; */ public final class CommonFunctions { + private static String filesDirectory = Main.getApplicationWorkDirectory() + File.separator + "files"; + + /** + * Creates all folders necessary for tests and normal operation + * + * @throws FileNotFoundException

If unable to create a folder

+ */ + public static void createAllFolders() throws FileNotFoundException { + createFolder(new File(filesDirectory)); + createFolder(new File(filesDirectory + File.separator + "Jars")); + createFolder(new File(filesDirectory + File.separator + "testjars")); + } + + /** + * Creates a given folder + * + * @param folder

The folder to create

+ * @throws FileNotFoundException

If unable to create the folder

+ */ + private static void createFolder(File folder) throws FileNotFoundException { + if (!folder.exists()) { + if (!folder.mkdirs()) { + throw new FileNotFoundException("Cannot create necessary directory."); + } + } + } + /** * Gets a resource as an InputStream * diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainerTest.java b/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainerTest.java index 7d39795..9756246 100644 --- a/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainerTest.java +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/server/ServerVersionContainerTest.java @@ -8,14 +8,21 @@ import org.junit.Test; import java.io.*; import static junit.framework.TestCase.assertEquals; +import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.createAllFolders; public class ServerVersionContainerTest { - private String versionFile = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "versions.csv"; + private final String filesDirectory = Main.getApplicationWorkDirectory() + File.separator + "files"; + private String versionFile = filesDirectory + File.separator + "versions.csv"; private ServerVersionContainer serverVersionContainer; @Before public void setUp() { + try { + createAllFolders(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } serverVersionContainer = ServerVersionContainer.getInstance(); }