【问题标题】:Java Web Start deploy on Windows startupJava Web Start 在 Windows 启动时部署
【发布时间】:2010-10-26 17:18:34
【问题描述】:

我有一个即将开始使用 Web Start 部署的 Java 应用程序。但是一个新的需求让我重新考虑了这一点,因为我现在需要添加一个功能,允许最终用户选择他们是否希望在启动时运行这个程序(Windows,而不是跨平台)。但我仍然希望避免将其作为服务运行。有什么方法可以使用 Web Start 来完成,还是我应该探索其他选项来部署它?提前致谢。

【问题讨论】:

    标签: java deployment java-web-start


    【解决方案1】:

    将 this 放入 jnlp 文件中实际上可以:

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

    但这仍然只适用于英文 Windows 版本。我认为德语是“Autostart”,西班牙语是“Iniciar”。所以它与通过IntegrationService的方式基本相同。

    【讨论】:

      【解决方案2】:

      我还没有尝试过,但我想知道您是否可以将新的 JNLP IntegrationServicejavaws 命令行程序结合使用。这个想法是以编程方式在 Windows 启动组中创建一个快捷方式(尽管该位置取决于特定的 Windows 版本)。

      【讨论】:

        【解决方案3】:

        要解决 Startup 文件夹的语言问题,只需使用注册表即可。这是一些应该可以工作的代码。这会调用 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;
            }
        }
        

        【讨论】:

        • 这行得通!可能有点慢(应用程序在几秒钟内没有响应,我认为这可能是因为这段代码),但是可以。现在我需要一些东西来让我的应用程序本身自动启动(而不是通过 JNLP 文件)......
        • 通过 SwingWorker 运行代码,它将在后台运行。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-20
        • 2013-05-02
        • 1970-01-01
        • 2012-10-06
        • 1970-01-01
        • 2012-12-10
        • 2012-09-16
        相关资源
        最近更新 更多