2014-01-13 120 views
1

我无法运行我的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*/ 
     } 
    } 
} 

我该怎么办?请帮帮我。

+0

谢谢忠曾黎 – Learner

+1

没有太多的信息去当谈到解决这个,但一般我会建议你看看Apache的通讯ons Exec(http://commons.apache.org/proper/commons-exec/),它使在Java中执行其他应用程序很少痛苦 –

+0

谢谢杰森,实际上我只是从网上复制粘贴代码,看看我是如何可以运行我的ssh-keygen.exe。而我读了你给我的文件。任何人都可以让我看看在java中执行.exe应用程序的工作代码。再次感谢杰森和所有帮助 – Learner

回答

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*/} 
    }  
} 
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*/} 
    } 


    }