2016-07-06 9 views
0

Javaを使用してこのファイルを実行/実行します。 パスは "/usr/local/studio.sh"Javaパスで特定のファイルを実行/実行するには

私が試している:

public class Main { 

public static void main(String[] args) { 

    String[] cmd = {"cd /usr/local","./studio.sh"}; 
    try { 
     Process p = Runtime.getRuntime().exec(cmd); 


    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} } 

が、それは動作しません。別の方法がありますか?

+0

あなたが書いたものを再読み込み: 'あなたは2つのコマンドを実行しようとしているように、CDは/ usr/local/studio.sh' – Siguza

+0

が見えます。プロセスのルートディレクトリを制御する場合は、フルパスを使用するか、ProcessBuilderを使用してそのディレクトリを設定します。 – copeg

+0

cmd配列では、最初の要素はコマンドであり、他のすべての要素は渡す引数です。これは実際には実行中です: '$ cd /usr/local/studio.sh。/ studio.sh' – CJxD

答えて

0

ProcessBuilderhttps://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.htmlを参照)を使用して、cdコマンドの代わりにdirectory(...)を使用して作業ディレクトリを設定できます。最後に

このような何か:

ProcessBuilder pb = new ProcessBuilder("/usr/bin/bash","/usr/local/studio.sh") 
pb.directory(...); 
pb.redirectErrorStream(true); 
Process p = pb.start(); 
IOUtils.copy(p.getInputStream(), System.out); 
p.waitFor(); 
0

これは私のために仕事の罰金です:

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

    String[] cmd = {"/usr/local/android-studio/bin/studio.sh"}; 
     Process p = Runtime.getRuntime().exec(cmd); 

     p.waitFor(); 

}

感謝。定義された「いいえ環境」:この場合

File folder = new File("/home/user/folder"); 
Process proc = Runtime.getRuntime().exec(command, null, folder); 

nullが示す:

0

Runtime APIを使用すると、メソッドを実行するディレクトリを示すためのメソッドを持っています。

Runtime API JavaDoc

関連する問題