Im new in the house but I am happpy to be here. Recently took my SCJP exam with a 85% score, thanks to the book SCJP by Kathy Sierra and Bert Bates.
Now I am an Head First Addict and its so much easier!
SO I am writing a litttle desktop application and I want my users to be able o choose to let the application start when windows starts or otherwise. I have already have the check option menu item in my menu list but how do I go about the real action?
You mean you want to have the option for the application to start when the user logs in? There isn't any point in starting a desktop application before that, because applications don't have access to the screen unless there is somebody logged in to look at the screen. Likewise logging off will cause applications to be closed.
Anyway the way you do that in Windows is to put a shortcut to your application in the user's Startup directory. However, the questions "Where is the user's Startup directory in Windows" and "How do I create a Windows shortcut" aren't Java questions at all. I don't know the answer to those questions, so I'm going to move the post to a more suitable forum.
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.
This is a lot different than what it was on the Windows 2000 machine I used to have. I expect that Microsoft's support site would have information on how this works on various versions of Windows.
Joined: Dec 20, 2010
Thanks for taking the time to reply Paul and Aditya.
I've found your last option very helpful Aditya. I guess I have to google up how to write a CMD file that does that. Alternatively, I would appreciate any helpful resource on that if have any. thanks alot!
@Paul: Thanks for that insight. I guess I would have to experiment on how it works with Windows versions prior to Windows 7 first then take it on from there.
Lolu Peace wrote:@Paul: Thanks for that insight. I guess I would have to experiment on how it works with Windows versions prior to Windows 7 first then take it on from there.
But usually, Windows programmers don't do that. They just install their programs with a Windows installer which knows all of that sort of thing and does it properly. If you've ever installed software on your Windows machine then you will be familiar with those installers.
Joined: Dec 20, 2010
True that. But you know with the windows installer thingy, its sort of like a once-done deal. Meanwhile, im trying to give my users the option of choosing if and when they want the application to start when windows starts or not.