Deploying Java Web Start at Windows Startup

I have a Java application that I am going to start using to start Web Start. But the new demand made me rethink this, because now I need to add functionality that allows the end user to choose whether they want to run this program at startup (Windows, not cross-platform). But I would still like to avoid making this run a service. Is there a way this can be achieved with Web Start, or should I explore other options for deploying it? Thanks in advance.

+3
source share
3 answers

Actually it works to put this in a jnlp file:

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

But it will still work only with English versions of Windows. The German "Autostart", the Spanish was "Iniciar", I think. Thus, it causes basically the same headache as the path through the IntegrationService.

+3
source

I have not tried it, but I am wondering if you can use the new JNLP IntegrationService in conjunction with the javaws command line program. The idea is to programmatically create a shortcut in the Windows startup group (although this location depends on the specific version of Windows).

+1
source

"", . . reg.exe .

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;
    }
}
+1

Source: https://habr.com/ru/post/1771548/


All Articles