使用 Java 启动外部程序?
问题:在一段时间后自动关闭程序。
解决方案:这是我解决问题的方法(在 Ubuntu 上与 Rhythmbox 配合使用):
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();
}
}
}
}
Problem: Automatically close a program after a certain amount of minutes.
Solution: Here is what I came up with to solve my problem (works with Rhythmbox on 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();
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能无法以这种方式启动 EXE。我认为这是为了使用默认应用程序打开文件,而不是自己运行应用程序 - 要么这样,要么您只是遇到了 Windows 安全问题。
您可以使用 Java.lang.Process 来运行带有参数的程序。
You probably can't launch an EXE that way. I think that was intended to open a file using the default app rather than running an app yourself - either that or you are just running into Windows security.
You can use the Java.lang.Process to run a program with arguments.
我不知道你如何设置广播电台。但我想我可以告诉你如何打开媒体播放器:
我调用了一次bat文件来更新我的IP地址,它对我来说效果很好。如果您设法调用 .bat,那么您可以忘记 java,并尝试启动播放器并向他发送参数。
这就是 java 调用 .bat 的方式:
这或多或少是 .bat 实现的样子:
start "" "%programfiles%\Windows Media Player\wmplayer.exe"
现在您只需要了解如何设置默认情况下,媒体播放器会在您想要的电台启动。
I have no clue how could you set the radio station. But i think i can tell you how to open the media player:
I called a bat file once to renew my ip adress and it worked fine for me. If you manage to call the .bat, then you can forget about java, and try to start the player and send him the parameters from there.
This is how java should call the .bat:
This is more or less the .bat implementation should looks like:
start "" "%programfiles%\Windows Media Player\wmplayer.exe"
Now you only need to find out how to set the media player to start at that station you want, by default.
Desktop.open() 不适用于运行可执行文件。您只需给它一个“数据文件”(例如 mysong.mp3 或 myvideo.avi),它就会使用系统的默认关联打开该文件。
如果您想运行 .exe,请使用 ProcessBuilder(搜索此站点,最近几天有很多关于 ProcessBuilder 的问题)
Desktop.open() is not intended to run executables. You just given it a "data file" (e.g. mysong.mp3, or myvideo.avi) and it will then open that file with the default association of the system.
If you want to run an .exe use a ProcessBuilder (search this site, there have been numerous questions regarding ProcessBuilder the last days)