现在人们可以做出许多假设(例如.desktop文件的内容),但也许最好有一个关于如何工作的逐步指南,例如:
测试程序
此SWT程序通过runtime.getRuntime().exec启动“gedit”。
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class EditorOpener {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setSize(500, 200);
shell.setText("Editor Opener");
shell.setLayout(new RowLayout());
final Button button = new Button(shell, SWT.PUSH);
button.setText("open gedit");
button.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
String[] cmdArray = new String[] { "/usr/bin/gedit" };
try {
Runtime.getRuntime().exec(cmdArray);
}
catch(IOException ex) {
ex.printStackTrace();
}
}
public void widgetDefaultSelected(SelectionEvent event) {
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
调用Java程序
Java程序是从shell脚本调用的,它看起来像这样:
#!/usr/bin/bash
java -cp /path/to/swt.jar:/path/to/classes EditorOpener
桌面文件
指向shell文件的.desktop文件如下所示:
[Desktop Entry]
Type=Application
Terminal=false
Name=Opener
Icon=utilities-terminal
Exec='/home/stephan/Documents/opener.sh'
Categories=Application;
结果
通过点击
打开Gedit
按钮,gedit在所有其他窗口上按预期启动。
演示
测试是在CentOS Linux 7上完成的:
gnome-shell --version
给出输出:
GNOME Shell 3.28.3