• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Start when windows starts

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 28330
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 28330
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 28330
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 4
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Runtime.exec() does the same thing as .bat file
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic