posted 13 years ago
You have to remember that since Java strives for platform-independence, performing any OS-specific task requires one to use native code and call it from Java via JNI. This, of course, makes your Java program bound to the OS, and platform-independence is lost.
Windows keeps a list of programs to be started on startup in Registry. Modifying this registry can be done by VB or VC++ APIs, and the relevant DLLs can then be invoked from Java via JNI. Of course, not the easiest job in the world, but if you want to do it programatically, this is the 'standard' procedure.
You can also use some of the 'Installer' applications for Java programs, which help you in creating shortcuts (on Desktop etc.), start Menu items etc. and can also configure your program to be run at Windows startup. Please note, though, that you won't be able to give user the option to control this behavior via a Menu action (or otherwise). User will have to choose (whether she wants to start the program at Windows startup or not) at the time of installation itself.
Having said that, since your requirement is just about starting/not starting your application at Windows startup, and you don't need other OS-specific tasks, you may have an easy solution. Windows has a Start Menu folder called 'Startup'. Any shortcuts inside this folder will be invoked at Windows startup. You can create a CMD file (command-prompt batch file) which starts your Java program in this folder to have it started automatically. Similarly, if User decides to not to start the application at Windows startup, programatically delete this file.
Hope this helps.