简体   繁体   English

终端命令在Runtime.exec()中失败

[英]Terminal command fails in Runtime.exec()

Not sure if this is best for Stackoverflow or Superuser since it pretty much applies to both... 不确定这是否最适合Stackoverflow或Superuser,因为这几乎适用于两者...

I'm running Lubuntu and I want to create a Windows-like 'start menu' search for the LXDE desktop menu. 我正在运行Lubuntu,我想创建一个类似于Windows的“开始菜单”搜索LXDE桌面菜单。 As with Windows, after an item has been found, I want to be able to launch it. 与Windows一样,找到项目后,我希望能够启动它。

The LXDE menu system works with .desktop files, so my code performs the following command using Runtime.exec() to start up the programs: LXDE菜单系统可处理.desktop文件,因此我的代码使用Runtime.exec()执行以下命令来启动程序:

gtk-launch <.desktop filename without extension>

This runs great for 99% of the time. 这在99%的时间内运行良好。 Unfortunately I cannot figure out why that other 1% refuses to launch. 不幸的是,我无法弄清楚为什么其他1%的人拒绝发射。 One of these applications is TeamViewer. 这些应用程序之一是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. 现在出现了一个奇怪的部分:当我在终端中运行gtk-launch teamviewer-teamviewer10命令时,它很好用,但是如果我通过Runtime.exec()运行相同的命令,它不会启动,但不会给出我也有任何错误消息。

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? 我需要修改gtk-launch命令还是我的代码有问题? (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. 解决方法是,我决定从.desktop文件中提取Exec命令,并通过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. 我仍然没有弄清楚为什么gtk-launch不适用于所有情况,但是对我来说,这种解决方法就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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