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?

Tiago Veloso
  • 8,513
  • 17
  • 64
  • 85
  • 1
    On what operating system are we talking about here? Windows? – thatidiotguy Jul 24 '13 at 15:38
  • @thatidiotguy - Good question, although I think we can infer that it's Windows by the fact that he's using a shortcut in the Startup folder. – Jim Jul 24 '13 at 15:40
  • 1
    How about adding your application to the Registry start? HKCU\Software\Microsoft\Windows\CurrentVersion\Run\ – Peter Jul 24 '13 at 15:41
  • @Tiago Velosa - What does your application do? Would it make sense as a Windows Service that can be automatically started by the Service Manager? Or does it have a UI, and you want it to just start like any other desktop application? – Jim Jul 24 '13 at 15:42
  • @Peter, how could I change the registry with code from within the application? Is that approach compatible with all versions of Windows? – Tiago Veloso Jul 24 '13 at 16:27
  • @TiagoVeloso I've reluctantly added an answer :P – Peter Jul 25 '13 at 08:19

3 Answers3

3

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

Community
  • 1
  • 1
Peter
  • 549
  • 7
  • 20
0

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

amicngh
  • 7,831
  • 3
  • 35
  • 54
0

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

Sireiz
  • 383
  • 3
  • 10