开发者

Launching External Programs with Java?

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();
        }
    }
}   

}


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.


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:

Runtime.getRuntime().exec("cmd /c start file.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() 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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜