在启动时启动Java应用程序 [英] Starting a Java application at startup

查看:193
本文介绍了在启动时启动Java应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java应用程序。

I have a Java application.

该应用程序有一个设置,用于决定应用程序是否在启动时启动。

The application has a setting that decides whether or not the application starts at startup.

目前,我有这是通过在StartUp项目文件夹中放置/删除快捷方式。

Currently, I have it this by placing/removing a shortcut in the StartUp items folder.

但是,我想知道是否有更好的方法来处理这种行为。

However, I am wondering if there is a better way to handle this behaviour.

编辑

是的,它是Windows。很抱歉没有在之前清除它。

Yes, it's Windows. Sorry for not clearing that before.

应用程序有一个用户可以触发操作的UI,应用程序也会在运行时定期在后台运行一些任务。

The application has an UI where the user may trigger actions, also the application runs a few tasks in the background periodically while running.

@Peter,我怎么能用应用程序中的代码更改注册表?这种方法是否与所有版本的Windows兼容?

@Peter, how could I change the registry with code from within the application? Is that approach compatible with all versions of Windows?

推荐答案

下面是一个小例子,说明如何从你的内部完成它应用程序

Below is a small example snippet of how it can be done from inside your application

static final String REG_ADD_CMD = "cmd /c reg add \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\" /v \"{0}\" /d \"{1}\" /t REG_EXPAND_SZ";
private void exec(String[] args) throws Exception
{
    if (args.length != 2)
        throw new IllegalArgumentException("\n\nUsage: java SetEnv {key} {value}\n\n");

    String key = args[0];
    String value = args[1];

    String cmdLine = MessageFormat.format(REG_ADD_CMD, new Object[] { key, value });

    Runtime.getRuntime().exec(cmdLine);
}

我很确定这适用于所有版本的Windows,因为它们全部使用相同的Startup \ Run注册表项。

I'm pretty sure this will work with all versions of Windows since they all use the same Startup\Run registry entry.

希望有所帮助! :)

信用卡

这篇关于在启动时启动Java应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆