在启动时启动Java应用程序

我有一个Java应用程序。

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

目前,我通过在启动项目文件夹中放置/删除快捷方式来实现这一点。

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

编辑

是的,这是Windows。 对不起,以前没有清除。

应用程序具有用户可以触发操作的UI,而且应用程序在运行时还会定期在后台运行几个任务。

@Peter,我怎么能改变registry与应用程序内的代码? 这种方法是否与所有版本的Windows兼容?

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

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

希望有所帮助! 🙂

信用

使用注册表在启动时启动你的程序,然后它将显示在由msconfig提供的列表中,通过Run 。 使用此注册表路径

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

在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