简体   繁体   English

Windows启动时的Java Web Start部署

[英]Java Web Start deploy on Windows startup

I have a Java application that I'm about to begin to use Web Start to deploy. 我有一个Java应用程序,我即将开始使用Web Start进行部署。 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). 但是一个新的需求让我重新思考这一点,因为我现在需要添加一些功能,允许最终用户选择他们是否想在启动时运行该程序(Windows,而不是跨平台) )。 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? 有没有办法可以使用Web Start完成,或者我应该探索其他选项来部署它? Thanks in advance. 提前致谢。

It actually works to put a this in the jnlp-file: 它实际上是把它放在jnlp文件中:

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

But that still would only work with English windows versions. 但这仍然适用于英文版Windows。 German is "Autostart", Spanish was "Iniciar" I think. 德语是“自动启动”,我认为西班牙语是“Iniciar”。 So it causes basically the same headache as the way via the IntegrationService. 因此它与通过IntegrationService的方式引起的问题基本相同。

To get around the language problem for the Startup folder just use the registry. 要解决Startup文件夹的语言问题,只需使用注册表。 Here is some code that should work. 这是一些应该有效的代码。 This calls reg.exe to make registry changes. 这会调用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;
    }
}

I have not tried it, but I wonder if you could use the new JNLP IntegrationService in combination with the javaws command line program. 我没有尝试过,但我想知道你是否可以将新的JNLP IntegrationServicejavaws命令行程序结合使用。 The idea being to programmatically create a shortcut in the Windows startup group (although that location is dependent on specific Windows version). 想法是以编程方式在Windows启动组中创建快捷方式(尽管该位置依赖于特定的Windows版本)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM