11
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaでOSコマンド実行時のメモ

Last updated at Posted at 2015-10-08

JavaでOSコマンド実行方法

java.lang.Runtimeのexecでコマンド実行する。

        String Command = "wget -D http://hogehoge.com/ --random-wait";
        Process p = Runtime.getRuntime().exec(Command);
        p.waitFor();

Javaでリダイレクトを含むOSコマンド実行方法

Runtime.execはリダイレクトの入ったコマンドを実行してくれないらしい。
なぜか知ってる人コメントほしいっす :sweat_smile:
そのため、shやcmdをコマンドを用いて実行する。

        String[] Command = {
                "sh",
                "-c",
                "grep http://hogehoge.com hogehoge.log >grep.txt"
        };

        // Windowsの場合はcmd /cをたたきます
        if( SystemUtils.IS_OS_WINDOWS ) {
        	Command[0] = "cmd";
        	Command[1] = "/c";
        }

        Process p = Runtime.getRuntime().exec(Command);
        p.waitFor();
11
9
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?