1

I want to know how can i create a file on jar file startup? Actually i need to know exact path of jar file and create my file if its not already exist i tried to use this but it has different results on my computer and my windows server!

String path = MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
    String decodedPath = URLDecoder.decode(path, "UTF-8").substring(1);

it turns me back current path on my computer but it turns "C:\" on my dedicated windows server

Peyman
  • 21
  • 3
  • 2
    Windows typically won't allow you to write to the application's directory (Assuming it's not in some write-able directory like `Users`). If you put your jar in Program Files, for example, you would need admin privileges to write there. Good practice is to write your files to the current user's directory. All that said, what's wrong with [this answer?](http://stackoverflow.com/questions/4032957/how-to-get-the-real-path-of-java-application-at-runtime) – Christopher Schneider Feb 24 '17 at 17:34
  • So if i put my jar file out of Users folder every thing should works right? also thank you for answering my first question in stackoverflow :P – Peyman Feb 24 '17 at 21:57
  • No. The location of your jar shouldn't matter. It should be portable. Windows user directory (like `C:\users\someAccount`) should pretty much always be writable. The answer mentions it, but it's `System.getProperty("user.home");`. On Windows 10 and Java 8, this gives me `C:\Users\Chris` – Christopher Schneider Feb 25 '17 at 00:11

1 Answers1

0

Here's how you can create a file on startup, and place it in same directory as the .jar:

private void createFile() throws Exception {
    File currentDir = new File(Preferences.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
    prefs = new File(currentDir.getParentFile().getAbsoluteFile() + "/hackemos-prefs.txt");
    if(!prefs.exists()) {
        prefs.createNewFile();

        String[] defaults = {
                "9", // number of items
                "100", // correct limit
                "0", // amount to get wrong
                "75", // delay, in ms
                "456,278", // default mouse position for start button
                "653,476", // default mouse position for copying text
                "686,615", // default mouse position for the text box
                "150,100", // Copy drag range
                "0" // mac mode, 1 = enabled
        };

        write(defaults);
    }
}

Here's how you can create a file on startup and place it in the Windows appdata folder (a good place for keeping things), or the Linux / Mac equivalent.

public static void initDirs() {
    String osName = Hardware.osName.toLowerCase();

    if(osName.contains("win")) {
        gameDir = new File((System.getenv("APPDATA") + File.separator + "Hidden" + File.separator));
    } else if(osName.contains("mac")) {
        gameDir = new File(System.getProperty("user.home") + "/Library/Application Support/Hidden"+File.separator);
    } else if(osName.contains("nux")) {
        gameDir = new File(System.getProperty("user.home"));
    }

    if(!gameDir.exists()) gameDir.mkdir();

     try {
        FileOutputStream fos = new FileOutputStream(gameDir + File.separator + fileName);
        ObjectOutputStream out = new ObjectOutputStream(fos);
        out.writeObject(object);
        out.close();
        fos.close();
        return true;
    } catch(Exception e) {
        e.printStackTrace();
        System.err.println("Couldn't save game.");
        return false;
    }
}

This is just some code from some of my old programs.

widavies
  • 774
  • 2
  • 9
  • 22
  • Well , i got more than what i need. thank you! it seems like a game code :D can i know what game ? – Peyman Feb 25 '17 at 00:04
  • No problem! Here's the game: https://www.youtube.com/watch?v=PkYXPXxMd7E&t=280s. I haven't worked on it in a long time though but it's something I hope to finish sometime. – widavies Feb 25 '17 at 03:16