Java 如果进程正在运行,请执行此操作,否则请执行该操作
我有一个只有一个按钮的小应用程序。我想让它做的是在按下按钮时检查进程是否正在运行。是的,做x,如果不是做y 这是我迄今为止的拙劣代码。我知道它只包括按钮和点击,其他什么都没有。这一点我真的很茫然Java 如果进程正在运行,请执行此操作,否则请执行该操作,java,linux,process,Java,Linux,Process,我有一个只有一个按钮的小应用程序。我想让它做的是在按下按钮时检查进程是否正在运行。是的,做x,如果不是做y 这是我迄今为止的拙劣代码。我知道它只包括按钮和点击,其他什么都没有。这一点我真的很茫然 public class toggle { public static void main(String[] args) { Display myDisplay = new Display(); Shell myShell = new Shell(myDisplay);
public class toggle {
public static void main(String[] args) {
Display myDisplay = new Display();
Shell myShell = new Shell(myDisplay);
myShell.setText("uTorrent Toggle");
myShell.setBounds(120, 120, 220, 120);
myShell.setLayout(new FillLayout());
final Button Start = new Button(myShell, SWT.PUSH);
Start.setText("Start uTorrent");
Start.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
Start.setText("Kill uTorrent");
try {
Runtime.getRuntime().exec("/bin/bash -c \"utorrent start\"");
} catch (IOException e) {
e.printStackTrace();
}
}
});
myShell.open();
while (!myShell.isDisposed()) {
if (!myDisplay.readAndDispatch()) myDisplay.sleep();
}
myDisplay.dispose();
}
}
要检查进程是否正在运行,可以使用“ps-efw”的输出,或者根据需要使用不同的参数