运行外部程序

时间:2014-01-13 00:53:58

标签: java runtime exe

我无法运行我的ssh-keygen.exe。输出表示构建成功但代码应执行.exe并显示应用程序。这是我的代码

import java.io.IOException;

public class SSHConnectPing {

    public static void main(String... args) throws IOException {
        try
        {
            Runtime.getRuntime().exec("C:\\ExecuteSSH\\ssh-keygen.exe");
        }
        catch(Exception exc)
        {
            System.out.println("error" + exc);/*handle exception*/
        }
    }
}

我该怎么办?请帮我。

2 个答案:

答案 0 :(得分:0)

感谢杰森,我现在可以执行我的.exe应用程序

我的代码现在是

package apacherunsshkeygen;

import java.io.IOException;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteWatchdog;


public class ApacheRunSSHKEygen {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {

    try {

//        String line = "AcroRd32.exe /p /h " + file.getAbsolutePath();
    String line = "C:\\ExecuteSSH\\ssh-keygen.exe";
    CommandLine cmdLine = CommandLine.parse(line);
    DefaultExecutor executor = new DefaultExecutor();

    //watchdog
    executor.setExitValue(1);
    ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
    executor.setWatchdog(watchdog);

    int exitValue = executor.execute(cmdLine);
    }

    catch (Exception exc){

       System.out.println("error" + exc);/*handle exception*/} 
    }


   }

答案 1 :(得分:0)

像我这样的Java新手正在研究如何执行外部应用程序(.exe),你可以试试这个样本:

// get apache.common.exec.jar at:
http://commons.apache.org/proper/commons-exec/download_exec.cgi

import java.io.IOException;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteWatchdog;


public class RunRsync {

public static void main(String[] args) throws IOException {

    try {

    //example :  String line = "C://file.exe";
    String line = "cmd /c start";   //you can put your .exe path here, like mine i run my window cmd
    CommandLine cmdLine = CommandLine.parse(line);
    DefaultExecutor executor = new DefaultExecutor(); 
    int exitValue = executor.execute(cmdLine);

    }
    catch (Exception exc){     
       System.out.println("error" + exc);/*handle exception*/} 
    }    
}