Search code examples
javaapachefileutils

can I get the directory of another file?


I have created an auto-updater for my java program. but at the end of the download it downloads the file to desktop. I can to get where is the program file and update it?

this is my code:

    @Override
    public void run() {
        if(!Debug) {
            try {
                FileUtils.copyURLToFile(new URL("fileurl"), new File(System.getenv("APPDATA") + "\\file.zip"));
                UnzipUtility unzip = new UnzipUtility();
                File deskfile = new File(System.getProperty("user.home") + "/Desktop/file.jar");
                if(deskfile.exists()) {
                    deskfile.delete();
                }

                unzip.unzip(System.getenv("APPDATA") + "\\file.zip", System.getProperty("user.home") + "/Desktop");
                File file = new File(System.getenv("APPDATA") + "\\file.zip");
                file.delete();
                Successfull = true;
            } catch (IOException e) {
                Successfull = false;
            }
            try {
                Updater.sleep(0L);
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    }

this is the code present in my external updater jar and I need to find the main program directory what can I do?

For example, if the main program is in any other directory except the Desktop, the update will always download it to the desktop ... I need it to change the file that executes the command to start the updater


Solution

  • If your external updater is in the same directory as your mail application you can use:

    System.getProperty("user.dir");

    This will give you the current folder. You can find more details here: https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html

    If this is not the case i see the following options:

    • ask the user before updating for the installation folder
    • store the installation folder in property file inOS user data // update jar folder.

    regards, WiPu