Java アプリから .bat ファイルを呼び出そうとしています。シンプルに見えますが、2 つの問題があり、アドバイスをいただければ幸いです。
以下のコードは、引数文字列が 94 文字を超えているため、bat ファイルの実行に失敗します。(文字列の末尾から末尾の「x」を削除することにより)正確に94文字を使用すると
clientArgs
、batファイルは正常に実行されます。
(別の呼び出しを使用して args を渡す場合と同じ問題String[]
)
とにかく、もっと長いコマンド文字列 (~150 文字) を渡すことができる必要があります。clientArgs
完全なファイル名ではなく短い名前のみを含める ように を変更すると (作業ディレクトリを指定しているため)、次のようになります。java.io.IOException: Cannot run program "client.bat" (in directory "C:\2\code\FlaFl\flafl-0.7-RC2"): CreateProcess error=2, The system cannot find the file specified
コードは次のとおりです。
public static void main(String[] args) {
String CMD_FILE_NAME = "client.bat"; //cmd or bat - still limit'n is 94 chars
String cmdFileFolder= "C:/2/code/FlaFl/flafl-0.7-RC2/";
File workingDir = new File(cmdFileFolder);
String clientArgs = cmdFileFolder +
CMD_FILE_NAME + " -host name12.mycompanyname3.com -app hive"
+ "123456789 34x"; //95 chars fails. remove the x to get it to work
System.out.println("length of invoc str is "+clientArgs.length());
Process process=null;
try {
process = Runtime.getRuntime().exec(clientArgs,null,workingDir);
} catch (IOException e) {
System.out.println("exception "+e);
}
//sleep(2000);
//if (process!=null)
// process.destroy();
}