问题:在一定的时间过后自动关闭程序。用Java启动外部程序?
解决方案:这里是我想出了解决我的问题(与工作的Rhythmbox在Ubuntu):
package rhythmBox;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class closeRhythmBox extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 400;
private static final int HEIGHT = 300;
private JTextField minutesBox;
public static void main(String[] args) {
new closeRhythmBox().setVisible(true);
}
public static void execKill(long minutes) throws InterruptedException {
Thread.sleep(minutes*60*1000);
try{
Runtime.getRuntime().exec("pkill rhythmbox");
System.exit(0);
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
public closeRhythmBox(){
setTitle("Rythmbox Timer");
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
pane.setLayout(new FlowLayout());
JPanel box = new JPanel();
box.setLayout(new FlowLayout());
JButton startButton = new JButton("Start");
startButton.addActionListener(this);
box.add(startButton);
box.add(new JLabel("Minutes Until Close"));
box.add(minutesBox = new JTextField(20));
pane.add(box);
pack();
}
public void actionPerformed(ActionEvent e) {
String textNum = minutesBox.getText();
long minuteNum = Long.parseLong(textNum);
if (e.getActionCommand().equals("Start")){
try {
execKill(minuteNum);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
假设你的意思是我只是打开一个文本文件,输入该命令,将其保存为bat文件,然后在我的Java程序中使用它?我想,不要试图打开,我应该更关注CLOSING,因为我可以简单地打开我的自我。我只需要在x分钟后终止这个过程。我会看看eclipse是否给了.exec()的逆操作,或者可能只是简单的看一下API。感谢大家的建议! – 2011-02-14 20:19:11