2

I have a Java application that lives in the system tray that I compile to a executable jar file. I would like to add the option within my program to add to the system startup items.

As I do not know of any uniform way to do this for all operating systems I assumed I would have to write code to do it for each one I intend to support so I started with Windows.

When I attempted to add it to the registry at [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run] using the code available here I discovered that under Windows 7 and 8 unless I have administrator privileges (by running from an elevated command prompt) my edits to the registry do not apply.

Then I spent a day trying to figure out how to get the Jar to relaunch itself with admin privileges before I gave up on that hacky workaround.

Can the task I'm trying to achieve even be accomplished and if so how?

Community
  • 1
  • 1
Fr33dan
  • 4,227
  • 3
  • 35
  • 62
  • possible duplicate of [Code for Auto starting a java application on windows startup](http://stackoverflow.com/questions/1025807/code-for-auto-starting-a-java-application-on-windows-startup) – Mike B Jan 06 '14 at 17:13
  • Have you looked at Apache [Commons Daemon](http://commons.apache.org/proper/commons-daemon/jsvc.html)? – Elliott Frisch Jan 06 '14 at 17:13
  • @ElliottFrisch I have not, but at a glance it seems that would require installing binary software onto the users computer. Ideally I'd like a solution that can be completely contained within my jar. – Fr33dan Jan 06 '14 at 17:38
  • @MikeB Not sure how that one didn't come up in my search. I found many others that were similar that mentioned the registry but I haven't heard of the "Autorun folder" they mention there. I will check that out. – Fr33dan Jan 06 '14 at 17:40
  • You probably want to describe **why** you want it to install for all users, otherwise you should register it under `HKEY_CURRENT_USER`, and get it for at least the current user. Next question is, of course, does it require elevation? If it does then you need to add it to the task scheduler for Windows 7 and newer – Anya Shenanigans Jan 06 '14 at 18:29
  • @Petesh I've considered making that distinction between current user login or system startup but I figured that will come when I see what is easiest on other platforms and then pick the one that will match for uniform behavior. For it's primary function my software does not need to be elevated. – Fr33dan Jan 06 '14 at 18:36
  • If you're trying to make something cross-platform, I'd stick with current user, as you'll bump up against the same issue on Linux and Mac OS X in relation to privileges. – Anya Shenanigans Jan 06 '14 at 18:39
  • @Petesh Well I finally caught your implication that the permissions for `HKEY_CURRENT_USER` are different than that of `HKEY_LOCAL_MACHINE`. Something so obvious I looked right past it. Switching to that works without elevation. I suppose I'll look to make Linux and OS X start on login as well since that seems to be all that is possible on Windows. If you post your suggestion as an answer I'll give you credit. – Fr33dan Jan 06 '14 at 18:49

2 Answers2

4

For the most part, you're actually looking to add the feature of auto starting on user login, rather than on system startup. For windows, if you add the registry entry under:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

This location does not suffer from permissions issues when run as an ordinary user, and has been supported for a long time under Windows (I'm thinking Windows 95 time frame here), so should be a safe change across all systems.

For Linux, assuming that the operating system is following the Open Desktop AutoStart specification, then you need to create the appropriate .desktop file in $HOME/.config/autostart/ and it should autostart on login in that case.

For Mac OS X, you need to create a launch agent plist in $HOME/Library/LaunchAgents. The Daemons and services documentation details how to construct this file.

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
  • This is perfect and covers all the systems I was looking for. I'll likely add some notification about Open Desktop AutoStart so my users can be aware if it doesn't work but should cover enough systems for me. Now to find an OS X machine to test on. – Fr33dan Jan 06 '14 at 20:06
0

Working around user privileges is not a good idea. The registry entry is the preferred way to go. Also keep in mind that the jar by itself is not executable, it requires the jvm, so what you might want to do is use a wrapper and register that.

AlfredoVR
  • 4,069
  • 3
  • 25
  • 33
  • I know, the registry entry I am creating is "javaw -jar [location of the jar file]". It works if the entry is created (and the user doesn't move the file which I warn about). But the default launching of a jar file by double clicking it does not actually edit the registry. Only if I run it as an administrator does the edit apply. I could put it in a wrapper but how would that get around the permissions issue? – Fr33dan Jan 06 '14 at 18:24
  • Just don't get around permissions, it's not a nice thing to do. Inform the user about requirement administrator privileges for such a thing, that's my advice. – AlfredoVR Jan 06 '14 at 18:38
  • I would, or rather the Windows UAC would when I relaunch if that solution had panned out but asking the user to run my software from an elevated command prompt not be acceptable. (There is no "Run as administrator" function for jar files) – Fr33dan Jan 06 '14 at 18:52