2

I have a Java application.

The application has a setting that decides whether or not the application starts at 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.

EDIT

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

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, how could I change the registry with code from within the application? Is that approach compatible with all versions of Windows?

4

3 回答 3

2

下面是一个小示例片段,说明如何从您的应用程序内部完成

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 注册表项。

希望有帮助!:)

信用

于 2013-07-25T08:16:31.047 回答
0

在 Windows 上,我使用开源Java Service Wrapper将我们的应用程序作为窗口服务,您可以在启动时自动设置它。

What you need to do is to download latest wrapper.exe and create wrapper.config file put all the configuration like Main class any VM arument other parameters in defined standards and create a window service by this exe

于 2013-07-24T15:42:15.027 回答
0

使用注册表在启动时启动您的程序,然后它将显示在msconfigcommnd through提供的列表中Run。使用此注册表路径

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

于 2013-07-24T18:48:44.843 回答