从Java中的按钮运行可执行程序

时间:2016-06-21 12:15:11

标签: java user-interface actionlistener

代码编译正常,但是当我按下按钮时,没有任何反应,你知道为什么吗?我想在按EXIF

时拨打EXIF软件
EXIF.addActionListener (new ActionListener(){
    public void actionPerformed (ActionEvent e) {
        try {     
            Runtime.getRuntime().exec("C:\\Program Files (x86)\\Exif Pilot\\ExifPilot.exe");  
        } catch(Exception exc) {
            /*handle exception*/
        }          
    }
});

1 个答案:

答案 0 :(得分:1)

问题在于执行的流程输出/输入流。当您使用java执行新进程时,它仅为其输入流分配8KB,因此您必须使用它。搜索进程gobblers

Windows也使用cmd /c来执行程序或更好:

String[] cmd = {"CMD", "/C", "C:\\Program Files (x86)\\Exif Pilot\\ExifPilot.exe"};

ProcessBuilder processBuilder = new ProcessBuilder(command);

BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((String line = br.readLine()) != null) {
    System.out.println(line);
}

try {
    int exitValue = process.waitFor();
    //TODO - do something with the exit value
} catch (InterruptedException e) {
    e.printStackTrace();
}