简体   繁体   中英

Terminal command fails in Runtime.exec()

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.)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM