Starting a Java application at startup

a 夏天 提交于 2019-11-29 12:50:31

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);
}

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

Hope that helps! :)

Credit

On Windows I have used open source Java Service Wrapper to make our application as window service which you can setup automatic at startup.

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

Use the Registry to start your program at the startup and then it will be shown in the list provided by msconfig commnd through Run. Use this registry path

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!