Reputation: 5808
My current attempt is:
File executionDir = new File("").getCanonicalFile();
ApplicationInfo info = ApplicationRegistry.getApplicationInfoByDir(executionDir);
return info.getVersion();
It works, but I wonder if there is a way to get at the information without making any assumptions about the current working directory?
Upvotes: 1
Views: 102
Reputation: 48070
You can query the sys.version
compiler variable:
import com.install4j.api.launcher.Variables;
try {
String version = Variables.getCompilerVariable("sys.version");
} catch (IOException e) {
// TODO not running in installation
}
Upvotes: 2
Reputation: 323
As a more complete working code sample, here is the update check from 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);
}
}
It uses the handy MessageWithLink
class from clickable links in JOptionPane to show a clickable link to the project releases page in the 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>"));
}
}
This is a very simple example of that can be done with install4j updaters but enough for our project.
Upvotes: 0