1

Not sure if this is best for Stackoverflow or Superuser since it pretty much applies to both...

I'm running Lubuntu and I want to create a Windows-like 'start menu' search for the LXDE desktop menu. As with Windows, after an item has been found, I want to be able to launch it.

The LXDE menu system works with .desktop files, so my code performs the following command using Runtime.exec() to start up the programs:

gtk-launch <.desktop filename without extension>

This runs great for 99% of the time. Unfortunately I cannot figure out why that other 1% refuses to launch. One of these applications is TeamViewer. Now here comes the strange part: When I run the command gtk-launch teamviewer-teamviewer10 in a terminal, it works great, but if I run the same command through Runtime.exec(), it does not start, but it does not give me any error messages either.

Here is my code:

Process p = Runtime.getRuntime().exec(new String[] { "gtk-launch", "teamviewer-teamviewer10" });
p.waitFor();

Do I have to modify the gtk-launch command or is there something wrong with my code? (Note: Almost all other programs launch just fine.)

Roland
  • 612
  • 5
  • 17
  • Have you checked if those programs which do not launch need anything from the environment, e.g. the DISPLAY, etc., which may be passed to them when you launch through the terminal but may not be when launched through Java? – RealSkeptic Aug 04 '15 at 16:13
  • That sounds plausible... How would I check this? Is there a way to see which variables are being used/passed by a specific terminal? – Roland Aug 04 '15 at 16:22
  • Well, you can see the environment of the terminal by typing `env`. But this is going to be a long list. It may be more feasible to try and hunt down the requirements in the documentation of that particular program. – RealSkeptic Aug 04 '15 at 16:25
  • I noticed that you are not printing the error messages that might come from gtk-launch or teamviewer. These might tell you exactly what is going wrong. You might want to write a short loop that reads from p.getErrorStream() and prints it out and run it in a separate thread. – WillShackleford Aug 04 '15 at 16:37
  • I used [this](http://stackoverflow.com/questions/5711084/java-runtime-getruntime-getting-output-from-executing-a-command-line-program) code to get the output and error output. It did not print anything useful. :/ – Roland Aug 04 '15 at 16:45

1 Answers1

0

As a workaround, I decided to extract the Exec command from the .desktop file and run this through bash.

String command = getExecCommandFromDesktopFile().replaceAll("\\\\ ", " ");
Runtime.getRuntime().exec(new String[] { "/bin/bash", "-c", command });

This seems to work under all conditions. I still haven't figured out why gtk-launch does not work for all cases, but for me, this workaround will do just fine.

Roland
  • 612
  • 5
  • 17