aspose file tools*
The moose likes General Computing and the fly likes Start when windows starts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » General Computing
Bookmark "Start when windows starts" Watch "Start when windows starts" New topic
Author

Start when windows starts

Lolu Peace
Greenhorn

Joined: Dec 20, 2010
Posts: 12
Hi everyone!
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?

thanks!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Locating the startup folder may be tricky too. For example on my Windows 7 machine the startup folder (at least when I log into it) is here:

C:\Users\clapha-p\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

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.
Lolu Peace
Greenhorn

Joined: Dec 20, 2010
Posts: 12
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
Lolu Peace
Greenhorn

Joined: Dec 20, 2010
Posts: 12
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.
Milhouse Cernilovsky
Greenhorn

Joined: Aug 23, 2010
Posts: 4

I was solving the same problem. You can put the information to make your app run on startup to the Windows registry. Here is the code (the second line must be surrounded by a try/catch block):



You can also get a reply (actually there are two InputStreams, one of them is for an error response):



Sometimes the regedit will ask you a question (y/n). You can answer the question via proc.getOutputStream() method.
Lolu Peace
Greenhorn

Joined: Dec 20, 2010
Posts: 12
Cool! I'l sure try this one out and let you know how it works.


Im still experimenting the batch file option...
Milhouse Cernilovsky
Greenhorn

Joined: Aug 23, 2010
Posts: 4

The Runtime.exec() does the same thing as .bat file
 
Consider Paul's rocket mass heater.
 
subject: Start when windows starts