2014-01-13 150 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*/} 
    } 


    }