我有一个Java / Eclipse应用程序,想要创建一个更新机制。
JRE位于Eclipse安装目录(Application / JRE)中,我不想使用Equinox p2机制。
我将必须执行以下步骤:
检查是否需要更新
下载并解压缩更新(包括JRE)
备份旧安装(在单独的目录中)
将解压缩的应用程序移至安装目录
开始申请
现在,由于虚拟机仍在运行,我担心无法移动JRE。
我还认为关闭挂钩可能无法正常工作,因为在运行中的VM上也会发生这种情况。
是否有人对这种用例有经验,完成JRE迁移的最佳实践是什么?
创建.bat文件并在退出前运行它?
最佳答案
我想出了一个解决方案,有一些实用程序类调用,但总的来说,我认为很清楚所要做的事情:
关键部分是:
Runtime.getRuntime()。exec(“ cmd / c start \”更新过程\“” + batFile);
如果批处理文件是在独立过程中启动的,则批处理文件本身将执行所有复制操作。
/**
* update the client installation, downloading update from server
*
* @return true if the update was successful
* @throws InvocationTargetException
* @throws InterruptedException
*/
protected boolean updateClient() throws InvocationTargetException, InterruptedException {
final File installDirectory = PathUtil.convertToFile(Platform.getInstallLocation().getURL());
if (!installDirectory.canWrite()) {
MessageDialog.openError(Display.getDefault().getActiveShell(), "Keine Schreibrechte",
"Keine Schreibrechte auf dem Installationsverzeichnis, Update kann nicht durchgeführt werden. Bitte benachrichtigen Sie einen Administrator.");
return false;
}
ProgressMonitorDialog dialog = new ProgressMonitorDialog(Display.getDefault().getActiveShell());
dialog.run(false, false, new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
monitor.setTaskName("Update wird heruntergeladen");
File updateDirectory = Files.createTempDirectory("update_").toFile(); //$NON-NLS-1$
File outputDirectory = new File(updateDirectory, "PackageClient"); //$NON-NLS-1$
String arch = System.getProperty("osgi.arch"); //$NON-NLS-1$
HttpURLConnection connection = ConnectionUtil.openConnection("update/PackageClient_" + arch + ".zip"); //$NON-NLS-1$ //$NON-NLS-2$
logger.info("download update from {}", connection.getURL()); //$NON-NLS-1$
logger.info("unpack update to {}", outputDirectory); //$NON-NLS-1$
InputStream in = connection.getInputStream();
ZipUtil.unzipToDirectory(outputDirectory, in, monitor);
in.close();
// write batch file for later execution, since not able to update running application and JRE
String launcher = System.getProperty("eclipse.launcher"); //$NON-NLS-1$
File batFile = new File(updateDirectory, "update.bat"); //$NON-NLS-1$
// create batch file which will create backup, delete original installation, copy update and start application
String batchContent = String.format(
"xcopy \"%1$s\" \"%2$s\\backup\" /i /s /h /e /y\r\n" //$NON-NLS-1$
+ "FOR /D %%%%p IN (\"%1$s\\*.*\") DO rmdir \"%%%%p\" /s /q\r\ndel /q \"%1$s\\*.*\"\r\n" //$NON-NLS-1$
+ "xcopy \"%3$s\" \"%1$s\" /i /s /h /e /y\r\ncd \"%1$s\"\r\n" + "start \"\" \"%4$s\"\r\n" //$NON-NLS-1$ //$NON-NLS-2$
+ "start /MIN \"\" cmd /c rmdir \"%2$s\" /s /q exit\r\nexit", //$NON-NLS-1$
installDirectory, updateDirectory, outputDirectory, launcher);
FileUtil.writeFile(batFile, batchContent);
logger.info("launching update batch file {}", batFile); //$NON-NLS-1$
// start update batch file in independent process
Runtime.getRuntime().exec("cmd /c start \"update process\" " + batFile); //$NON-NLS-1$
} catch (Exception e) {
throw new InvocationTargetException(e);
}
}
});
return dialog.getReturnCode() == Window.OK;
}
这里是创建的批处理文件的示例:
xcopy "D:\temp\test\eclipse" "D:\temp\fdo\update_3081617184889033760\backup" /i /s /h /e /y
FOR /D %%p IN ("D:\temp\test\eclipse\*.*") DO rmdir "%%p" /s /q
del /q "D:\temp\test\eclipse\*.*"
xcopy "D:\temp\fdo\update_3081617184889033760\PackageClient" "D:\temp\test\eclipse" /i /s /h /e /y
cd "D:\temp\test\eclipse"
start "" "D:\temp\test\eclipse\Application.exe"
start /MIN "" cmd /c rmdir "D:\temp\fdo\update_3081617184889033760" /s /q exit
exit
关于java - 在更新过程中移动JRE,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31969393/