26
29

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からShellScriptを実行

Last updated at Posted at 2015-11-30

実行環境

OS: Mac OS X 
$ java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

コード

Main.java
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main (String args[]) {
        try {
            Process process = new ProcessBuilder("ls", "-l").start();
            InputStreamReader isr = new InputStreamReader(process.getInputStream(), "UTF-8");
            BufferedReader reader = new BufferedReader(isr);
            StringBuilder builder = new StringBuilder();
            int c;
            while ((c = reader.read()) != -1) {
                builder.append((char)c);
            }
            System.out.println("result:\n" + builder.toString());
            System.out.println("Command return code: " + process.waitFor());
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

githubリポジトリ

https://github.com/atsushi-ito-110/execute-shell-script
(結論としてはこれがすべて)

参考サイト

追記

作成日(2015/11)から2.5年くらい経っていて、今(2018/06)だったらどうすっかなと思い記事をリファクタリングしました。
なにかあればissue作成してもらえたら喜びます。

26
29
0

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
26
29

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?