How Can a JAR File Delete Itself?

别等时光非礼了梦想. 提交于 2019-12-03 20:57:52

Here is a working example

public class Delete
{
    public static void main(String[] args) throws Throwable
    {
        System.out.println("Running");

        System.out.println("Deleting");
        Runtime.getRuntime().exec("cmd /c ping localhost -n 6 > nul && del Delete.jar");

        System.out.println("Ending");
        System.exit(0);
    }
}

The main feature powering this is cmd /c ping localhost -n 6 > nul && del Delete.jar

It deletes the jar file after waiting 5 seconds

To test, I used javac Delete.java && jar cfe Delete.jar Delete Delete.class to build the jar, and java -jar Delete.jar to run

I also verified that I could not delete the jar file while it was executing, through new File("Delete.jar").delete(); and also using Windows Explorer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!