Java, cannot delete file on Windows


I have a simple updater for my application. In code i am downloading a new version, deleting old version and renaming new version to old.

It works fine on Linux. But doesn't work on Windows. There are no excepions or something else. p.s. RemotePlayer.jar it is currently runned application.


Doesn't work - it means that after file.delete() and file.renameTo(...) file still alive. I use sun java 7. (because I use JavaFX).

p.s. Sorry for my English.

public void checkUpdate(){
    new Thread(new Runnable() {
        public void run() {
            System.err.println("Start of checking for update.");
            StringBuilder url = new StringBuilder();
            File curJarFile = null;
            File newJarFile  = null;

            try {
                curJarFile = new File(new File(".").getCanonicalPath() + "/Player/RemotePlayer.jar");
                newJarFile = new File(new File(".").getCanonicalPath() + "/Player/RemotePlayerTemp.jar");
                if (newJarFile.exists()){
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                System.err.println("Cannot find curr Jar file");

            if (curJarFile.exists()){
                try {
                    String resp = NetworkManager.makeGetRequest(url.toString());
                    JSONObject jsresp = new JSONObject(resp);
                    if (jsresp.getString("st").equals("ok")){
                        if (jsresp.getInt("size") != curJarFile.length()){
                            System.out.println("New version available, downloading started.");
                            StringBuilder downloadURL = new StringBuilder();

                            if (NetworkManager.downLoadFile(downloadURL.toString(), newJarFile)){

                                if (jsresp.getString("md5").equals(Tools.md5File(newJarFile))){
                                    System.err.println("Deleting old version. File = " + curJarFile.getCanonicalPath());

                                    boolean b = false;
                                    if (curJarFile.canWrite() && curJarFile.canRead()){
                                    }else System.err.println("Cannot delete cur file, doesn't have permission");

                                    System.err.println("Installing new version. new File = " + newJarFile.getCanonicalPath());

                                    if (curJarFile.canWrite() && curJarFile.canRead()){
                                        b = true;
                                    }else System.err.println("Cannot rename new file, doesn't have permission");

                                    System.err.println("last version has been installed. new File  = " + newJarFile.getCanonicalPath());

                                    if (b){
                                        Platform.runLater(new Runnable() {
                                            public void run() {
                                                JOptionPane.showMessageDialog(null, String.format("Внимание, %s", "Установлена новая версия, перезапустите приложение" + "", "Внимание", JOptionPane.ERROR_MESSAGE));

                                }else System.err.println("Downloading file failed, md5 doesn't match.");
                        } else System.err.println("You use latest version of application");
                }catch (Exception e){
                    System.err.println("Cannot check new version.");

            }else {
                System.err.println("Current jar file not found");

private void setAccesToFile(File f){
    f.setReadable(true, false);
    f.setExecutable(true, false);
    f.setWritable(true, false);
12/3/2012 3:16:31 PM

Accepted Answer

windows locks files that are currently in use. you cannot delete them. on windows, you cannot delete a jar file which your application is currently using.

12/3/2012 2:48:52 PM

I found the solution to this problem. The problem of deletion occurred in my case because-:

File f1=new File("temp.txt");
RandomAccessFile raf=new RandomAccessFile(f1,"rw");
f1.delete();//The file will not get deleted because raf is open on the file to be deleted

But if I close RandomAccessFile before calling delete then I am able to delete the file.

File f1=new File("temp.txt");
RandomAccessFile raf=new RandomAccessFile(f1,"rw");
f1.delete();//Now the file will  get deleted

So we must check before calling delete weather any object such as FileInputStream, RandomAccessFile is open on that file or not. If yes then we must close that object before calling delete on that file.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow