2015-10-28 37 views
1

我有一個java類,在我需要的服務器上運行任何腳本,並且它很好地工作。以編程方式從特定位置運行腳本

我有一個問題,但是,我需要從一個特定的位置運行一個腳本,不知道該怎麼做。在我的java方法中運行我的腳本之前,有沒有辦法「cd」到目錄?

我目前的方法:

public String runScriptFile(String pathname) throws IOException{ 
    Runtime rt = Runtime.getRuntime(); 
    String output=""; 
    Process proc = rt.exec(pathname); 

    BufferedReader stdInput = new BufferedReader(new 
     InputStreamReader(proc.getInputStream())); 

    BufferedReader stdError = new BufferedReader(new 
     InputStreamReader(proc.getErrorStream())); 

    String s = null; 
    while ((s = stdInput.readLine()) != null) { 
     output+=s; 
    } 

    while ((s = stdError.readLine()) != null) { 
     output+=s; 
    } 
    return output; 
} 
+0

也許這可以幫助你:http:// stackoverf low.com/questions/26697916/running-a-bash-command-in-different-directory-from-a-java-program – Tobi

+0

是的,這確實是我最終解決它的方式,可能是一個重複的帖子所以如果你想回答我會把你的標記標記爲正確的。 – Fearghal

回答

1

的答案是使用位置PARAM中的Runtime.exec如下

Process proc = rt.exec(pathname, null, new File("C:\\files\\")); 
相關問題