2016-04-15 15 views
0

作業ディレクトリを最初に変更する必要があります。次に、.txtファイルにあるいくつかのパラメータでバッチファイルを実行します。バッチファイルを開くために検索しましたが、作業ディレクトリを変更してから実行してパラメータを渡すことはできません。作業ディレクトリを変更してから、パラメータをJavaで実行するバッチファイルを実行してください

のJavaスニペット:コマンドプロンプトで

final String dosCommand = "cmd /c start cmd.exe /K"; 
     final String location = "\"C:/Program Files/.../abc.bat"; 
     final String trail = "d:\\xyz.txt"; 
     try { 
     final Process process = Runtime.getRuntime().exec(
      dosCommand + " " + location + " " + "pro_wait" + " " + trail); 
     final InputStream in = process.getInputStream(); 
     int ch; 
     while((ch = in.read()) != -1) { 
      System.out.print((char)ch); 
     } 
     } catch (IOException e) { 
     e.printStackTrace(); 
     } 

D:\>"C:\Program Files\...\abc.bat" pro_wait d:\xyz.txt 

が、私はJAVAからの完全なコマンドを実行することができません、と私はコマンドを実行しています。助けてください。 おかげさまですべてに感謝します!

+0

'...'はWindows NTでは何も意味しません。 Win 9xでは、Novell OSとの互換性のために2つの祖先を意味しました。 –

+1

'cmd/c" cd DIRECTORYとabc.bat "がそれを行います。 – saka1029

+0

あなたは見ましたか?http://stackoverflow.com/questions/840190/changing-the-current-working-directory-in-java –

答えて

0

dosCommandから "start"を削除し、バッチファイルの引用符が完全であることを確認してください。 次に、execのコマンドで作業ディレクトリを指定することができます。

final String dosCommand = "cmd /c cmd.exe /K"; 
    final String location = "\"C:/Program Files/.../abc.bat\""; 
    final String trail = "d:\\xyz.txt"; 
    try { 
     final Process process = Runtime.getRuntime().exec(
       dosCommand + " " + location + " " + "pro_wait" + " " + trail, 
       null, new File("D:\\")); 
     final InputStream in = process.getInputStream(); 
     int ch; 
     while ((ch = in.read()) != -1) { 
      System.out.print((char) ch); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
+0

ありがとうございました。 – LeoNad

関連する問題