如何获取当前正在执行的启动器的版本?

问题描述 投票:0回答:2

我目前的尝试是:

File executionDir = new File("").getCanonicalFile();
ApplicationInfo info = ApplicationRegistry.getApplicationInfoByDir(executionDir);
return info.getVersion();

它有效,但我想知道是否有一种方法可以在不对当前工作目录做出任何假设的情况下获取信息?

java install4j
2个回答
2
投票

可以查询

sys.version
编译器变量:

import com.install4j.api.launcher.Variables;

try {
    String version = Variables.getCompilerVariable("sys.version");
} catch (IOException e) {
    // TODO not running in installation
}

0
投票

作为更完整的工作代码示例,这里是来自 https://jaerproject.org 的更新检查:

    public static String INSTALL4J_UPDATES_URL = "https://raw.githubusercontent.com/SensorsINI/jaer/master/updates.xml";

   public static void checkForInstall4jReleaseUpdate(Component parent) {
        // check if rujning from installed version of jaer (fails if running from git compiled jaer)
        String currentVersion="unknown";
        try {
            currentVersion = Variables.getCompilerVariable("sys.version");
        } catch (IOException e) {
            // TODO not running in installation
            JOptionPane.showMessageDialog(parent, "<html> Could not determine current version. <p>To check for udpates, you need to install jAER with an install4j installer. <p>(Probably are you running from git compiled development environment): <p>" + e.toString(), "Version check error", JOptionPane.ERROR_MESSAGE);
            return;
        }

        String updateUrl = INSTALL4J_UPDATES_URL;
        try {
            UpdateDescriptor updateDescriptor = UpdateChecker.getUpdateDescriptor(updateUrl, ApplicationDisplayMode.GUI);
            if (updateDescriptor.getPossibleUpdateEntry() != null) {
                // TODO an update is available, execute update downloader
                UpdateDescriptorEntry updateDescriptorEntry = updateDescriptor.getEntryForCurrentMediaFileId();
                String updateVersion = updateDescriptorEntry.getNewVersion();
                JOptionPane.showMessageDialog(parent, 
                        new MessageWithLink("<html>Current version: "+currentVersion+"<p> Update " + updateVersion + 
                                " is available; see <a href=\"https://github.com/SensorsINI/jaer/releases\">jAER releases</a>"), 
                                "Update available", JOptionPane.INFORMATION_MESSAGE);
//                JOptionPane.showMessageDialog(parent, "<html>Update " + updateVersion + " is available; see <a href=\"https://github.com/SensorsINI/jaer/releases\">jAER releases</a>", "Releases update check", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(parent, new MessageWithLink("<html>No update available;<br> you are running current release " + currentVersion+"<p>See <a href=\"https://github.com/SensorsINI/jaer/releases\">jAER releases</a>"), "No update available", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (IOException | UserCanceledException e) {
            JOptionPane.showMessageDialog(parent, "Could not check for release update: " + e.toString(), "Update check error", JOptionPane.ERROR_MESSAGE);
        }
    }

它使用 JOptionPane 中

可点击链接中方便的 
MessageWithLink 类来显示 JOptionPane 中项目发布页面的可点击链接。

/** * * Usage: JOptionPane.showMessageDialog(null, new MessageWithLink("Here is a * link on <a href=\"http://www.google.com\">http://www.google.com</a>")); * */ public class MessageWithLink extends JEditorPane { private static final long serialVersionUID = 1L; public MessageWithLink(String htmlBody) { super("text/html", "<html><body style=\"" + getStyle() + "\">" + htmlBody + "</body></html>"); addHyperlinkListener(new HyperlinkListener() { @Override public void hyperlinkUpdate(HyperlinkEvent e) { if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) { // Process the click event on the link (for example with java.awt.Desktop.getDesktop().browse()) // System.out.println(e.getURL() + " was clicked"); if (Desktop.isDesktopSupported()) { try { Desktop.getDesktop().browse(e.getURL().toURI()); } catch (IOException|URISyntaxException ex) { JOptionPane.showMessageDialog(null, ex.toString(),"Error",JOptionPane.ERROR_MESSAGE); } } } } }); setEditable(false); setBorder(null); } static StringBuffer getStyle() { // for copying style JLabel label = new JLabel(); Font font = label.getFont(); Color color = label.getBackground(); // create some css from the label's font StringBuffer style = new StringBuffer("font-family:" + font.getFamily() + ";"); style.append("font-weight:" + (font.isBold() ? "bold" : "normal") + ";"); style.append("font-size:" + font.getSize() + "pt;"); style.append("background-color: rgb(" + color.getRed() + "," + color.getGreen() + "," + color.getBlue() + ");"); return style; } public static void main(String[] args) { JOptionPane.showMessageDialog(null, new MessageWithLink("Here is a link on <a href=\"http://www.google.com\">http://www.google.com</a>")); } }
这是一个非常简单的示例,可以使用 install4j 更新程序完成,但对于我们的项目来说已经足够了。

© www.soinside.com 2019 - 2024. All rights reserved.