1

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.

UPDATED:

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() {
        @Override
        public void run() {
            System.err.println("Start of checking for update.");
            StringBuilder url = new StringBuilder();
            url.append(NetworkManager.SERVER_URL).append("/torock/getlastversionsize");
            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()){
                    newJarFile.delete();
                }
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                System.err.println("Cannot find curr Jar file");
                return;
            }

            if (curJarFile.exists()){
                setAccesToFile(curJarFile);
                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();
                            downloadURL.append(NetworkManager.SERVER_URL).append("/torock/getlatestversion");

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

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

                                    boolean b = false;
                                    if (curJarFile.canWrite() && curJarFile.canRead()){
                                        curJarFile.delete();
                                    }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()){
                                        newJarFile.renameTo(curJarFile);
                                        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() {
                                            @Override
                                            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){
                    e.printStackTrace();
                    System.err.println("Cannot check new version.");
                }

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

private void setAccesToFile(File f){
    f.setReadable(true, false);
    f.setExecutable(true, false);
    f.setWritable(true, false);
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Vetalll
  • 3,472
  • 6
  • 24
  • 34
  • If you test the path through your program will it find the file? I suspect you just have a pathing issue. – Max Dec 03 '12 at 15:00
  • I made a many checkings special for finding answers. And i sure - File exists. file.canRead() and file.canWrite() - returns true – Vetalll Dec 03 '12 at 15:06
  • 1
    Since your app. is apparently a desktop app., I suggest deploying it using [Java Web Start](http://stackoverflow.com/tags/java-web-start/info) which *"..provides many appealing features including, but not limited to, splash screens, desktop integration, file associations, **automatic update** (including lazy downloads and programmatic control of updates), partitioning of natives & other resource downloads by platform, architecture or Java version, configuration of run-time environment (minimum J2SE version, run-time options, RAM etc.), easy management of common resources using extensions.."* – Andrew Thompson Dec 03 '12 at 15:13
  • Pounded on this for 2 days - found my solution here: https://stackoverflow.com/questions/991489/file-delete-returns-false-even-though-file-exists-file-canread-file-canw?noredirect=1&lq=1 – Jeff Levine Jun 03 '18 at 13:22

6 Answers6

7

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");
raf.close();
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.

Tushar Arora
  • 103
  • 1
  • 4
3

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.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
1

Since you are using Java 7, try java.nio.file.Files.delete(file.toPath()), it'll throw exception if deletion fails.

irreputable
  • 44,725
  • 9
  • 65
  • 93
0

There are several reasons:

  1. Whether you have permissions to edit the file in windows.

  2. The file is in use or not.

  3. The path is right or not.

Max
  • 5,799
  • 3
  • 20
  • 27
Healer
  • 290
  • 1
  • 7
  • I made a many checkings special for finding answers. And i sure - File exists. file.canRead() and file.canWrite() - returns true – Vetalll Dec 03 '12 at 15:13
0

I don't know wich version of Java you are using.

I know when Java was sun property they publish that the Object File can't delete files correctly on windows plateform (sorry I don't find the reference no more).

The tricks you can do is to test the plateform directly. When you are on linux just use the classic File object.

On windows launch a command system to ask windows to delete the file you want.

Runtime.getRuntime().exec(String command);
David Level
  • 353
  • 2
  • 16
-3

I just want to make one comment. I learned that you can delete files in Java from eclipse if you run eclipse program as Administrator. I.e. when you right click on the IDE Icon (Eclipse or any other IDE) and select Run as Administrator, Windows lets you delete the file.

I hope this helps. It helped me.

Cordially,

Fernando

Fernando
  • 9
  • 3