~ and ${USER} don't work in .desktop files

Ubuntu 12.04 32-bit

I am trying to manually create a .desktop file to launch an application. I wish to execute the command:

java -jar ~/.osbuddy/osbuddy.jar

When I run my .desktop file. However, this does not work. I then tried:

java -jar /home/${USER}/.osbuddy/osbuddy.jar

which did not work either.

Finally,

java -jar /home/branon/.osbuddy/osbuddy.jar

worked fine. I manually executed all of the above lines through the terminal and it launched the program fine. Why won't it work in a .desktop file?

Thanks for any light you can shed. This is my first time on AskUbuntu so I apologise if I'm ignoring some customary rules of this form. If I've transgressed, please correct me.


Solution 1:

The command in a .desktop file is not run by a shell but using some system call. Because of that you can't use shell syntax like ~ or ${USER} there

Solution 2:

As Florian Diesch says, tilde expansion and parameter expansion is not supported in launchers (i.e., .desktop files). But if you need to use them--or other shell expansions--you can do so by making the launcher execute a shell with command-line arguments telling the shell to run a specific command and exit.

Thus you can make your command:

sh -c 'java -jar ~/.osbuddy/osbuddy.jar'

I've used sh (which in Ubuntu is dash) rather than bash) for efficiency and minimalism. But if you needed an advanced feature provided by bash, you could use that instead.

If the path ~/.osbuddy/osbuddy.jar is valid for multiple users and multiple users will be using the launcher, I recommend the sh -c (or if you prefer, bash -c) way. However, if this is only for you and your main goal is to avoid typing /home/brannon...well, passing the command to sh -c involves some extra typing, too.