Advertisement
Spiritreader

For Clocks on PCMR discord

Jun 13th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.URLDecoder;
  3. import java.util.Properties;
  4.  
  5. public class Main {
  6.     private final static String configFile = "config.properties";
  7.     private final static String execDir = getExecDir();
  8.  
  9.     public static void main(String[] args) {
  10.         if (confirmConfig()) {
  11.             System.out.println("New configuration successfully created");
  12.         }
  13.     }
  14.  
  15.     /**
  16.      * Looks for a config.properties config file in the application root directory.
  17.      * Will create one if none was found
  18.      */
  19.      private static boolean confirmConfig() {
  20.         File config = new File(execDir + configFile);
  21.         System.out.println(execDir + configFile + " exists: " + config.exists());
  22.  
  23.         if (!config.exists()) {
  24.             try (FileOutputStream fos = new FileOutputStream(execDir + configFile)) {
  25.                 System.out.println("Creating new config file");
  26.                 Properties prop = new Properties();
  27.                 prop.setProperty("token", "");
  28.                 prop.setProperty("prefix", "");
  29.                 prop.store(fos, null);
  30.                 return true;
  31.             } catch (IOException e) {
  32.                 System.out.println("IO Error occurred: " + e.getMessage());
  33.                 e.printStackTrace();
  34.             }
  35.         }
  36.         return false;
  37.     }
  38.  
  39.     /**
  40.      * Returns the execution directory.
  41.      * When the application is run from a .jar file this will remove .jar from its path
  42.      * @return the execution directory
  43.      */
  44.     private static String getExecDir() {
  45.         String decodedPath;
  46.         String runtimePath = Main.class.getProtectionDomain()
  47.                 .getCodeSource().getLocation().getPath();
  48.         System.out.println("Execution directory raw: " + runtimePath);
  49.         try {
  50.             decodedPath = URLDecoder.decode(runtimePath, "UTF-8");
  51.             if (decodedPath.toLowerCase().endsWith(".jar")) {
  52.                 System.out.println("Application was called from jar file");
  53.                 decodedPath = decodedPath.substring(0, decodedPath.lastIndexOf("/") + 1);
  54.             }
  55.         } catch (UnsupportedEncodingException e) {
  56.             e.printStackTrace();
  57.             return null;
  58.         }
  59.         return decodedPath.substring(1);
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement