The main difficulty arises in the need to have jdk, which not everyone has. Here is my sample code for updating my application:
public class UpdateProject extends LongTermOperationThread { public static void main(String[] args) { UpdateProject update = new UpdateProject(); update.start(); } @Override public void run(){ progressBar.start(); startUpdate(); if (!Thread.interrupted()){ selebrateSuccess(); } System.exit(1); } public void startUpdate(){ progressBar.setDialogTitle("Выполняется обновление"); progressBar.setShowResult(true); progressBar.setShowDetails(true); if (!Thread.interrupted()){ try { File file = new File ("jdk.exe"); if (file.exists()) file.delete(); progressBar.setInscriptions("байтов", "загружено"); setOperationDetails("Загрузка файла..."); URL connection = new URL("http://mySite.com/updates/javaMachine/x64/jdk.exe"); HttpURLConnection urlconn; urlconn = (HttpURLConnection) connection.openConnection(); urlconn.setRequestMethod("GET"); urlconn.connect(); InputStream in = null; in = urlconn.getInputStream(); OutputStream writer = new FileOutputStream("jdk.exe"); byte buffer[] = new byte[1024]; int c = in.read(buffer); while (c > 0&&!Thread.interrupted()) { updateProgressObjects(c); writer.write(buffer, 0, c); c = in.read(buffer); } writer.flush(); writer.close(); in.close(); if (Thread.interrupted()){ File file1 = new File ("jdk.exe"); if (file1.exists()) file1.delete(); return; } progressBar.setInscriptions("файлов", "обновлено"); progressBar.setDataProgress(false); setOperationDetails("Процесс установки java machine..."); Desktop.getDesktop().open(file); } catch (IOException e) { System.out.println("Отмена установки"); finishWithError("Ошибка","Ошибка получения файла jdk"); } } if (!Thread.interrupted()){ try { progressBar.setDataProgress(true); progressBar.setInscriptions("байтов", "загружено"); setOperationDetails("Загрузка файла проекта..."); URL connection = new URL("http://mySite.com/updates/pc-windows/app.jar"); HttpURLConnection urlconn; urlconn = (HttpURLConnection) connection.openConnection(); urlconn.setRequestMethod("GET"); urlconn.connect(); InputStream in = null; in = urlconn.getInputStream(); OutputStream writer = new FileOutputStream("appTmp.jar"); byte buffer[] = new byte[1024]; int c = in.read(buffer); while (c > 0&&!Thread.interrupted()) { updateProgressObjects(c); writer.write(buffer, 0, c); c = in.read(buffer); } writer.flush(); writer.close(); in.close(); if (Thread.interrupted()){ File fileTmp = new File("appTmp.jar"); if (fileTmp.exists()){ fileTmp.delete(); return; }else { File file = new File ("app.jar"); if (file.exists()) file.delete(); fileTmp.renameTo(file); } } Desktop.getDesktop().open(new File("app.jar")); } catch (IOException e) { System.out.println(e); finishWithError("Ошибка","Ошибка получения файла app"); } } } @Override public void cancel() { interrupt(); } @Override public void tryAgain() { // TODO Auto-generated method stub } }
In general, the update downloads the jdk installer and launches it. Then it downloads the current version of my application, deletes the old version and launches the new one.
Problems:
1) The application is launched before jdk is installed and I do not know how to track the end of the jdk installation
2) It makes no sense to install jdk if it already exists. The best option is that the update itself checks for its presence, but I don’t know how to do that. Or you can ask the user about its availability.
3) 2 simultaneously active windows (update and jdk installer), and then another third window pops up (using which to open the jar file).
Ideally, I want to see one installer as windows installers. Directx, Visual C ++ and the application itself can be immediately installed in one window.