2017-12-09 17 views
0

はどうすればWindowsの10の環境では、Javaから、UbuntuでのBashで書かれた、シェルスクリプトを実行できますか?
私はこのコードを使用しようとしていますが、スクリプトを実行したり実行したりしていません。Windows 10環境でJavaからUbuntuのBashで書かれたシェルスクリプトを実行するにはどうしたらいいですか?

public static void main(String[] args) throws IOException { 
     Runtime rt = Runtime.getRuntime(); 
     ProcessBuilder builder = new ProcessBuilder(
      "bash.exe", "/mnt/d/Kaldi-Java/kaldi-trunk/tester.sh"); 

     Process p = builder.start(); 
     BufferedReader r = new BufferedReader(new 
     InputStreamReader(p.getInputStream())); 
     String line; 
     while (true) { 
      line = r.readLine(); 
      if (line != null) { System.out.print(line);} 
      else{break;} 
     } 
    } 
+0

bash.exeは、完全修飾する必要があります、あなたは冗長なランタイムを定義しています... – Ivonet

答えて

0

まず、コマンドラインからこのコマンドを実行しようとしましたか?もしあなたがそれを働かせれば、問題はウィンドウ上のbashではなくあなたのjavaプログラムであることを意味します。

あなたは、コマンドラインから実行することができない場合は、最初にこの問題を解決

I Ubuntuを使用しますが、このようななめらかにしようとするあなたをアドバイスすることができますので、私は私のプログラムをテストすることはできません

(プログラムが終わるまで待ちます)
public class Main { 

    public static void main(String[] args) throws IOException, InterruptedException { 
     ProcessBuilder builder = new ProcessBuilder(
       "bash.exe", "/mnt/d/Kaldi-Java/kaldi-trunk/tester.sh"); 

     Process p = builder.start(); 

     /* waitFor() method stops current thread until this process is over */ 
     p.waitFor(); 
     // I think that scanner is a nicer way of parsing output 
     Scanner scanner = new Scanner(p.getInputStream()); 
     while (scanner.hasNextLine()) { 
      // you do not have to create `line` outside the loop 
      // it does not change performance of a program 
      String line = scanner.nextLine(); 
      System.out.println(line); 
     } 
    } 
} 
0

Windows環境でJavaを使用してスクリプトを実行しようとしている場合は、私が異なり、それを実行する提案します。

私はあなたがここで尋ねた貴重な質問からコード適応している:

How to run Unix shell script from Java code?

また、この質問は、あなたの質問をお届けします。 Unable to read InputStream from Java Process (Runtime.getRuntime().exec() or ProcessBuilder)

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

    Process proc = Runtime.getRuntime().exec(
      "/mnt/d/Kaldi-Java/kaldi-trunk/tester.sh"); 
    BufferedReader read = new BufferedReader(
      new InputStreamReader(proc.getInputStream())); 

    while (read.ready()) 
    { 
     System.out.println(read.readLine()); 
    } 

} 

私は、これは何であると信じてあなたは探している。私は、あなたのJavaコードがちょっと残っていて、これらの編集があなたに役立つはずだと信じています。 Java実行可能ファイルをビルドしたら、Windowのコマンドプロンプトから実行することができます。

関連する問題