2011-06-24 107 views
-1

我存儲在某些Linux系統.sh文件。該文件的完整路徑是:Java代碼來執行sh文件

/comviva/CPP/Kokila/TransactionHandler/scripts/stopTH.sh 

我特林通過

Runtime.getRuntime().exec(`/comviva/CPP/Kokila/TransactionHandler/scripts/stopTH.sh`) 

執行它,但它拋出一些異常。

我想從在MS-Windows環境下我的Java程序中執行該文件;可能嗎?

+6

這是不可能在Windows本地運行在Unix shell腳本,這是你想要什麼做 - 你需要將Unix shell腳本轉換(不只是重命名)到Windows批處理文件(帶有.bat或.cmd文件擴展名/後綴)。拋出哪個異常?您使用的是哪個版本的Java? – 2011-06-24 06:04:18

+2

您提到** ** Linux系統**和** Windows系統。 shell腳本位於你的Linux系統上,對嗎?而您的Java應用程序(應該啓動shell腳本)位於Windows系統上。那是對的嗎? –

+4

什麼是「一些例外」。這是一個重要的信息! – dmeister

回答

2

這是我的代碼。正如評論所說,在Linux上工作,在Windows(XP)上失敗。 AFAIK與Windows的問題是,cmd.exe是奇怪的關於它的參數。對於您的特定子任務,您可能可以通過使用引號進行操作並將子任務參數嵌入到子任務本身中。

/** Execute an abritrary shell command. 
    * returns the output as a String. 
    * Works on Linux, fails on Windows, 
    * not yet sure about OS X. 
    */ 
public static String ExecuteCommand(final String Cmd) { 
    boolean DB = false ; 
    if (DB) { 
     Debug.Log("*** Misc.ExecuteCommand() ***"); 
     Debug.Log("--- Cmd", Cmd); 
    } 
String Output = ""; 
String ELabel = ""; 
    String[] Command = new String[3]; 
    if (Misc.OSName().equals("WINDOWS")) { 
     Command[0] = System.getenv("ComSPec"); 
     Command[1] = "/C"; 
    } else { 
     Command[0] = "/bin/bash"; 
     Command[1] = "-c"; 
    } 
Command[2] = Cmd; 
    if (DB) { 
     Debug.Log("--- Command", Command); 
    } 
    if (Misc.OSName().equals("WINDOWS")) { 
     Debug.Log("This is WINDOWS; I give up"); 
     return ""; 
    } 
try { 
     ELabel = "new ProcessBuilder()"; 
     ProcessBuilder pb = new ProcessBuilder(Command); 
     ELabel = "redirectErrorStream()"; 
     pb.redirectErrorStream(true); 
     ELabel = "pb.start()"; 
     Process p = pb.start(); 
     ELabel = "p.getInputStream()"; 
     InputStream pout = p.getInputStream(); 
     ELabel = "p.waitFor()"; 
     int ExitCode = p.waitFor(); 
     int Avail; 
     while (true) { 
      ELabel = "pout.available()"; 
      if (pout.available() <= 0) { 
       break; 
      } 
      ELabel = "pout.read()"; 
      char inch = (char) pout.read(); 
      Output = Output + inch; 
     } 
     ELabel = "pout.close()"; 
     pout.close(); 
    } catch (Exception e) { 
     Debug.Log(ELabel, e); 
    } 

    if (DB) { 
     Debug.Log("--- Misc.ExecuteCommand() finished"); 
    } 
    return Output; 
} 

}

+5

對於Java中的方法和變量,您不應該有大寫的第一個字符。 –

1

我做了一個快速測試在這裏,下面的工作假設你有/斌/慶典你的機器上:

我/tmp/test.sh:

#!/bin/bash 
echo `ls` 

我的Java代碼:

try { 
    InputStream is = Runtime.getRuntime().exec("/bin/bash /tmp/test.sh").getInputStream(); 
    int i = is.read(); 
    while(i > 0) { 
     System.out.print((char)i); 
     i = is.read(); 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

輸出:在當前目錄下的所有文件

編輯:我有點被忽視,「從窗口中執行」的評論。我不知道你的意思。

3

要執行Windows上的.sh腳本,你必須安裝合適的命令解釋器。例如,您可以在您的Windows機器上安裝Cygwin環境,並使用它的bash解釋器。

但是,Windows甚至沒有使用Cygwin Linux操作系統。有些腳本不會在沒有改動的情況下從一個環境移植到另一個環境。如果我在Linux環境下通過Java執行腳本時遇到問題,我寧願在該環境中調試該問題。

記住,你可以在調試模式下在Linux上啓動Java程序和Windows中的IDE調試器附加到遠程進程。

2

感謝大家的回覆。我找到了解決問題的辦法。對於這種情況,我們需要將我的Windows機器綁定到Linux系統。以下是有效的代碼:

public String executeSHFile(String Username, String Password,String Hostname) 
    { 
     String hostname = Hostname; 
     String username = Username; 
     String password = Password; 
     try{ 
      Connection conn = new Connection(hostname); 
       conn.connect(); 
       boolean isAuthenticated = conn.authenticateWithPassword(username, password); 
       if (isAuthenticated == false) 
       throw new IOException("Authentication failed."); 
       Session sess = conn.openSession(); 
       sess.execCommand("sh //full path/file name.sh"); 

       System.out.println("Here is some information about the remote host:"); 
       InputStream stdout = new StreamGobbler(sess.getStdout()); 
       BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); 
       while (true) 
       { 
        String line = br.readLine(); 
        if (line == null) 
         break; 
        current_time=line; 
        System.out.println(line); 
       } 

       System.out.println("ExitCode: " + sess.getExitStatus()); 
       /* Close this session */ 

       sess.close(); 

       /* Close the connection */ 

       conn.close(); 
     }catch(IOException e) 
     { 
      e.printStackTrace(System.err); 
      System.exit(2); 
     }finally{ 

     } 
} 

謝謝。

+1

您使用'Connection' /'Session'類:它們來自哪個庫? –

0

你可以製作一個.bat文件(批處理文件),它可以在Windows上運行。 放在.bat文件中.SH文件的內容 從應用程序啓動一個進程,如:

Process.Start("myFile.bat");