开发者

Java Web Start deploy on Windows startup

开发者 https://www.devze.com 2023-01-21 21:15 出处:网络
I have a Java application that I\'m about to begin to use Web Start to deploy. But a new demand has made me rethink this, as I\'m now required to add a piece of functionality that allows the end user

I have a Java application that I'm about to begin to use Web Start to deploy. But a new demand has made me rethink this, as I'm now required to add a piece of functionality that allows the end user to select whether or not they'd like to run this program on startup (of Windows, not cross-platform). But I'd still like to shy away from making this run as a service. Is there any way that this can be accomplished using Web Start, or should I explore开发者_开发知识库 other options to deploy this? Thanks in advance.


It actually works to put a this in the jnlp-file:

<shortcut online="true">
    <desktop/>
    <menu submenu="Startup"/>
</shortcut>

But that still would only work with English windows versions. German is "Autostart", Spanish was "Iniciar" I think. So it causes basically the same headache as the way via the IntegrationService.


I have not tried it, but I wonder if you could use the new JNLP IntegrationService in combination with the javaws command line program. The idea being to programmatically create a shortcut in the Windows startup group (although that location is dependent on specific Windows version).


To get around the language problem for the Startup folder just use the registry. Here is some code that should work. This calls reg.exe to make registry changes.

public class StartupCreator {

    public static void setupStartupOnWindows(String jnlpUrl, String applicationName) throws Exception {
        String foundJavaWsPath = findJavaWsOnWindows();
        String cmd = foundJavaWsPath + " -Xnosplash \"" + jnlpUrl + "\"";
        setRegKey("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", applicationName, cmd);
    }

    public static String findJavaWsOnWindows() {
    // The paths where it will look for java
    String[] paths = {
        // first use the JRE that was used to launch this app, it will probably not reach the below paths
        System.getProperty("java.home") + File.separator + "bin" + File.separator + "javaws.exe",
        // it must check for the 64 bit path first because inside a 32-bit process system32 is actually syswow64
        // 64 bit machine with 32 bit JRE
        System.getenv("SYSTEMROOT") + File.separator + "syswow64" + File.separator + "javaws.exe",
        // 32 bit machine with 32 bit JRE or 64 bit machine with 64 bit JRE
        System.getenv("SYSTEMROOT") + File.separator + "system32" + File.separator + "javaws.exe",};
        return findJavaWsInPaths(paths);
    }

    public static String findJavaWsInPaths(String[] paths) throws RuntimeException {
        String foundJavaWsPath = null;
        for (String p : paths) {
            File f = new File(p);
            if (f.exists()) {
                foundJavaWsPath = p;
                break;
            }
        }
        if (foundJavaWsPath == null) {
            throw new RuntimeException("Could not find path for javaws executable");
        }
        return foundJavaWsPath;
    }

    public static String setRegKey(String location, String regKey, String regValue) throws Exception {
        String regCommand = "add \"" + location + "\" /v \"" + regKey + "\" /f /d \"" + regValue + "\"";

        return doReg(regCommand);
    }

    public static String doReg(String regCommand) throws Exception {

        final String REG_UTIL = "reg";
        final String regUtilCmd = REG_UTIL + " " + regCommand;
        return runProcess(regUtilCmd);
    }

    public static String runProcess(final String regUtilCmd) throws Exception {
        StringWriter sw = new StringWriter();
        Process process = Runtime.getRuntime().exec(regUtilCmd);

        InputStream is = process.getInputStream();
        int c = 0;
        while ((c = is.read()) != -1) {
            sw.write(c);
        }
        String result = sw.toString();
        try {
            process.waitFor();
        } catch (Throwable ex) {
            System.out.println(ex.getMessage());
        }
        if (process.exitValue() == -1) {
            throw new Exception("REG QUERY command returned with exit code -1");
        }
        return result;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消