如何在Java Swing应用程序中引入自动更新和重新启动功能。 此外,我需要回滚到以前的版本。 我制作了一个应用程序jar文件和启动程序jar文件,它启动了应用程序jar文件。 这次尝试很成功。但是,在为MacOSX创建安装程序时,我无法集成这两个jar文件。
Launcher class
如下:
public class Launcher {
private final Logger logger = Logger.getLogger(Launcher.class.getName());
private String getVersionNumber() throws IOException {
try {
JarFile runningJarFile = new JarFile(new File("Application.jar"));
String versionNumber = runningJarFile.getManifest()
.getMainAttributes().getValue("Bundle-Version");
runningJarFile.close();
logger.log(
Level.SEVERE,
new StringBuilder()
.append("The version number of existing Application.jar file is ")
.append(versionNumber).toString());
return versionNumber;
} catch (IOException e) {
logger.log(
Level.SEVERE,
new StringBuilder()
.append("Could not read the version number from existing Application.jar")
.append(Arrays.toString(e.getStackTrace()))
.toString());
throw new IOException(e);
}
}
private void updateApplication() {
try {
File updateDirectory = new File("Update");
if (updateDirectory.isDirectory()) {
if (updateDirectory.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.startsWith("\\.");
}
}).length > 0) {
String versionNumber = getVersionNumber();
logger.log(
Level.SEVERE,
new StringBuilder()
.append("A new update is available. Rename the existing Application.jar to ")
.append(versionNumber)
.append(".jar")
.append(" and rename the new_Application.jar to Application.jar")
.toString());
File Application = new File("Application.jar");
Application.renameTo(new File(new StringBuilder()
.append(versionNumber).append(".jar").toString()));
File newApplication = new File("Update/new_Application.jar");
newApplication.renameTo(new File("Application.jar"));
newApplication.delete();
}
}
} catch (Exception e) {
logger.log(Level.SEVERE,
new StringBuilder().append("Could not update Application")
.append(Arrays.toString(e.getStackTrace()))
.toString());
}
}
private void launchApplication() {
try {
logger.log(Level.SEVERE, "Lauching Application.jar");
ProcessBuilder pb = new ProcessBuilder("Java", "-jar", "Application.jar");
pb.start();
} catch (IOException e) {
logger.log(Level.SEVERE,
new StringBuilder().append("Could not launch Application.jar ")
.append(Arrays.toString(e.getStackTrace()))
.toString());
}
}
private void quitLauncher() {
logger.log(Level.SEVERE, "Launcher is exiting");
System.exit(0);
}
private void startApplication() {
updateApplication();
launchApplication();
quitLauncher();
}
public static void main(String[] args) {
Launcher launcher = new Launcher();
launcher.startApplication();
}
}
由于
答案 0 :(得分:1)
首先,您是在创建标准安装程序包(mpkg还是pkg)?
您应该让启动器下载更新,然后执行它(使用/ usr / sbin / installer)。之后,安装程序应该有一个postflight / postinstall / postprocessing脚本,可以使用你的名字杀死任何应用程序(这可能与寻找具有给定名称的进程一样棘手......),然后启动最近安装的应用程序。
这使得您的应用程序将在第一次安装后启动(您可以避免这样做,以检查它是否是更新或不从Launcher中保存标记文件“updating.txt”可能已足够 - )< / p>
以下是如何创建安装程序包(几乎是临时表单)的完整指南:Making OS X Installer Packages like a Pro - Xcode Developer ID ready pkg
或者,您可以使用这个很酷的工具:http://s.sudre.free.fr/Software/Packages/about.html
我希望这对你所寻找的东西来说并不过分。