Java Web Start 在 Windows 启动时部署

发布于 2024-09-29 23:23:53 字数 183 浏览 4 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栀梦 2024-10-06 23:23:53

实际上,将 this 放入 jnlp 文件中是可行的:

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

但这仍然只适用于英文 Windows 版本。我认为德语是“Autostart”,西班牙语是“Iniciar”。因此,它会引起与通过 IntegrationService 的方式基本上相同的头痛。

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.

悲念泪 2024-10-06 23:23:53

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

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).

断念 2024-10-06 23:23:53

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文