Starting a Java application at startup

吃可爱长大的小学妹 提交于 2019-11-28 06:21:56

问题


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?


回答1:


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




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/17838671/starting-a-java-application-at-startup

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