Vinyl
Vinyl

Reputation: 333

Custom uninstaller unable to self destruct in Java

I am trying to make a custom uninstaller using Java, which I can turn into a console application using Launch4J. What I am trying to achieve is once the uninstaller finishes , it needs to open a command prompt which will delete it and then delete the installation folder. However, I am only getting this error: The process cannot access the file because it is being used by another process, yet I can see that the uninstaller has already been deleted, but the installation directory has not. Could it be the uninstaller is still open in memory, or what can I do to solve this?


public static void main(String[] args) {
    String command;
    command = "\"";
    command += "echo Self Destruction Commenced.";
    command += "& del /f /q \"" + System.getProperty("user.dir") + "\\uninstaller.exe\"";
    command += "& rmdir /q /s \"" + System.getProperty("user.dir") + "\"";
    command += "& echo. & echo MyProgram successfully uninstalled...";
    command += "& set /p exitkey= \"Press [ENTER] key to finish...\" ";
    command += "\"";
    selfDestructProgram("start cmd.exe /c " + command);
}

private static void selfDestructProgram(String command) {
    ProcessBuilder processBuilder = new ProcessBuilder();
    processBuilder.command("cmd.exe", "/c", command);
    try {
        Process process = processBuilder.start();
        process.waitFor();
    } catch (Exception var3) {
        var3.printStackTrace();
    }
}

Upvotes: 1

Views: 34

Answers (1)

Allovalinks
Allovalinks

Reputation: 26

The installation directory is the process that is still open in the new cmd. What you need to do is to cd to the parent dir of the installation dir using the newly opened cmd, and then you will be able to delete it. Use the code below instead:

public static void main(String[] args) {
    //get the path to the parent directory
    File workingDir = new File(System.getProperty("user.dir"));
    String parentDir = workingDir.getParent();

    String command;
    command = "\"";
    command += "echo Self Destruction Commenced.";
    command += "& del /f /q \"" + System.getProperty("user.dir") + "\\uninstaller.exe\"";
    command += "& rmdir /q /s \"" + System.getProperty("user.dir") + "\"";
    command += "& echo. & echo MyProgram successfully uninstalled...";
    command += "& set /p exitkey= \"Press [ENTER] key to finish...\" ";
    command += "\"";

    //first cd to this parent directory before deleting the child directory, which is the installtion directory.
    selfDestructProgram("cd /d " + parentDir + " & start cmd.exe /c " + command);
}

private static void selfDestructProgram(String command) {
    ProcessBuilder processBuilder = new ProcessBuilder();
    processBuilder.command("cmd.exe", "/c", command);
    try {
        Process process = processBuilder.start();
        process.waitFor();
    } catch (Exception var3) {
        var3.printStackTrace();
    }
}

At the moment the installation directory cannot be deleted since it is open in the cmd, making it locked from deletion or renaming or some other file operations.

Upvotes: 1

Related Questions