Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.URLDecoder;
- import java.util.Properties;
- public class Main {
- private final static String configFile = "config.properties";
- private final static String execDir = getExecDir();
- public static void main(String[] args) {
- if (confirmConfig()) {
- System.out.println("New configuration successfully created");
- }
- }
- /**
- * Looks for a config.properties config file in the application root directory.
- * Will create one if none was found
- */
- private static boolean confirmConfig() {
- File config = new File(execDir + configFile);
- System.out.println(execDir + configFile + " exists: " + config.exists());
- if (!config.exists()) {
- try (FileOutputStream fos = new FileOutputStream(execDir + configFile)) {
- System.out.println("Creating new config file");
- Properties prop = new Properties();
- prop.setProperty("token", "");
- prop.setProperty("prefix", "");
- prop.store(fos, null);
- return true;
- } catch (IOException e) {
- System.out.println("IO Error occurred: " + e.getMessage());
- e.printStackTrace();
- }
- }
- return false;
- }
- /**
- * Returns the execution directory.
- * When the application is run from a .jar file this will remove .jar from its path
- * @return the execution directory
- */
- private static String getExecDir() {
- String decodedPath;
- String runtimePath = Main.class.getProtectionDomain()
- .getCodeSource().getLocation().getPath();
- System.out.println("Execution directory raw: " + runtimePath);
- try {
- decodedPath = URLDecoder.decode(runtimePath, "UTF-8");
- if (decodedPath.toLowerCase().endsWith(".jar")) {
- System.out.println("Application was called from jar file");
- decodedPath = decodedPath.substring(0, decodedPath.lastIndexOf("/") + 1);
- }
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- return null;
- }
- return decodedPath.substring(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement