JavaアプリケーションからOSコマンドを起動する
戻る

/**
* $Id: CommandInvocation.html,v 1.1 2009/06/22 16:11:40 kishi Exp kishi $
* IEなどのブラウザをJavaアプリケーションから起動してみる
*/

public class BrowserInvoker {

    /** サブプロセスの終了を待つか */
    private boolean waitsFor = false;
    private String url;
    /** デフォルト値 */
    private String browserPath = "C:\\Program Files\\Internet Explorer\\iexplore.exe";

    public void setURL( String url ) {
        this.url = url;
    }

    public void setWaitsFor( boolean waitsFor ) {
        this.waitsFor = waitsFor;

    }

    public void setBrowserPath( String browserPath ) {
        this.browserPath = browserPath;
    }

    public void execute() {

        try {
            Runtime runtime = Runtime.getRuntime();

            // IEの実行
            Process process = runtime.exec( browserPath + " " + url );

            if ( waitsFor ) {
                // サブプロセス終了の待ち合わせ
                process.waitFor();

                // 終了コードを確認する
                System.out.println( "終了コードは、" + process.exitValue() );
            }
        } catch ( Exception exception ) {
            exception.printStackTrace();
        }
    }

    static public void main( String[] args ) {
        BrowserInvoker bi = new BrowserInvoker();

        bi.setWaitsFor( false );
        bi.setURL( "http://www.sun.co.jp/" );
        bi.execute();

        bi.setWaitsFor( true );
        bi.setURL( "http://www.oracle.co.jp/" );
        bi.execute();
    }

}

戻る inserted by FC2 system