Javaからアプリケーションを実行する方法
Javaからアプリケーションを実行する方法を調べてみた。
ひしだま's ホームページ「外部プロセス起動」に詳しく載ってます。
分かったこと
- ProcessBuilderクラスのcommandメソッドの引数にList<String>を渡すとき、空白で区切った値を混在させると、エラーになった。
ffmpegを実行させたとき”Incompatible sample format '(null)' for codec 'libmp3lame', auto-selecting format 's16'”というエラーとなった。DLLを別にしたも使っていたのでパスが通ってないのかと思いパスを通してみたら”C:\tools\ffmpeg\bin\ffmpeg.exe: missing argument for option 'i sm14538995_low.mp4 -ar 48000 -ab 128 -f mp3 sm14538995_low.mp3'”となったりした。
空白で区切らずListの要素として全てばらせばOK - 標準出力をリダイレクションで出力するときは、同時に実行することはできない模様。たぶん一番最初に実行したものだけ生き残る。
ためしにDOS窓で複数同時実行してみても最初に実行した一つだけが残り、他の二つはファイルが使われているので実行できないとかエラーに。
マルチスレッドにすれば動くかと思ったけれど、DOS窓で実行した結果を考えるとそれも怪しい。
以下はリダイレクションなしで結果もみないでffmpegを実行しっぱなしでさっさとJavaのプロセスを終了するもの。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package apprun;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author kotatuinu
*/
public class AppRun {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ProcessBuilder pb = new ProcessBuilder();
/*
// ffmpegの実行結果をリダイレクションでファイルに出力
// この場合、waitFor()しないと複数ファイルの処理が行われない
String[] command = new String[16];
command[0] = "cmd";
command[1] = "/c";
command[2] = "C:\\tools\\ffmpeg\\bin\\ffmpeg.exe";
command[3] = "-i";
command[4] = ""; //.mp4
command[5] = "-ar";
command[6] = "48000";
command[7] = "-ab";
command[8] = "128";
command[9] = "-f";
command[10] = "mp3";
command[11] = ""; //.mp3
command[12] = ">>";
command[13] = "op.txt";
command[14] = "2>>";
command[15] = "op2.txt";
*/
// 出力結果を
String[] command = new String[10];
command[0] = "C:\\tools\\ffmpeg\\bin\\ffmpeg.exe";
command[1] = "-i";
command[2] = ""; //.mp4
command[3] = "-ar";
command[4] = "48000";
command[5] = "-ab";
command[6] = "128";
command[7] = "-f";
command[8] = "mp3";
command[9] = ""; //.mp3
for (String arg : args) {
// 以下のように引数を空白で分けたモノを使うのはエラーになる
//command[3] = String.format("-i %s.mp4 -ar 48000 -ab 128 -f mp3 %s.mp3", arg, arg); <- これだめ
command[2] = String.format("%s.mp4", arg);
command[9] = String.format("%s.mp3", arg);
for(String c : command){
System.out.print(c);
System.out.print(" ");
}
System.out.println();
pb.command(command);
try {
Process pr = pb.start();
//pr.waitFor();
//System.out.printf("process retunr %d\n", pr.exitValue());
// } catch (InterruptedException ex) {
// Logger.getLogger(AppRun.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(AppRun.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println("end.");
}
}
« 今週届いたもの | トップページ | 2011年第4四半期新作アニメ初見感想 »
This web site is certainly relatively useful because I’m in the second making a web floral internet site - even though I'm only commencing out consequently it is truly pretty tiny, nothing at all similar to this web site. Can hyperlink to some in the posts right here because they are very. Many thanks a lot. Zoey Olsen
投稿: diablo 3 pre order | 2011年11月 7日 (月) 16時53分