加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Executing System commands in Java---ref

发布时间:2020-12-14 06:18:16 所属栏目:Java 来源:网络整理
导读:One of the nice features of Java language is that it provides you the opportunity to execute native system commands and in this tutorial we will see how to use Runtime class in a quite simple program to execute commands like ipconfig As an

One of the nice features of Java language is that it provides you the opportunity to execute native system commands and in this tutorial we will see how to use Runtime class in a quite simple program to execute commands like ipconfig

As an example consider the below snippet

<span style="color: #0000ff;">import<span style="color: #000000;"> java.io.BufferedReader;
<span style="color: #0000ff;">import<span style="color: #000000;"> java.io.IOException;
<span style="color: #0000ff;">import<span style="color: #000000;"> java.io.InputStreamReader;

<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> SystemCommandsTest {

</span><span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;static</span> <span style="color: #0000ff;"&gt;void</span><span style="color: #000000;"&gt; main(String args[]) {
    StringBuffer output </span>= <span style="color: #0000ff;"&gt;new</span><span style="color: #000000;"&gt; StringBuffer();
    </span><span style="color: #0000ff;"&gt;try</span><span style="color: #000000;"&gt; {
        Process process </span>=<span style="color: #000000;"&gt; Runtime.getRuntime().exec(
                </span>"ipconfig"<span style="color: #000000;"&gt;);
        BufferedReader reader </span>= <span style="color: #0000ff;"&gt;new</span> BufferedReader(<span style="color: #0000ff;"&gt;new</span><span style="color: #000000;"&gt; InputStreamReader(
                process.getInputStream()));

        String line </span>= ""<span style="color: #000000;"&gt;;
        </span><span style="color: #0000ff;"&gt;while</span> ((line = reader.readLine()) != <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;) {
            output.append(line </span>+ "n"<span style="color: #000000;"&gt;);
        }
        System.out.println(</span>"OUTPUT:"+<span style="color: #000000;"&gt;output.toString());
    } </span><span style="color: #0000ff;"&gt;catch</span><span style="color: #000000;"&gt; (IOException e) {
        </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; TODO Auto-generated catch block</span>

<span style="color: #000000;"> e.printStackTrace();
}

}

}

As you can see in the above class we first need to access the runtime environment.This is done with

Runtime.getRuntime()

method.We also need to create an OS process in order to execute the system command. As you can see this process is created with

exec()

method of the Runtime class.Finally we need to create a BufferedReader in order to read the input stream of the process and display the output in our console.

Executing ipconfig command will give us the below output

Windows IP Configuration

Ethernet adapter Local Area Connection 2<span style="color: #000000;">:

    Connection</span>-<span style="color: #000000;"&gt;specific DNS Suffix  . : 

    IP Address. . . . . . . . . . . . : </span>192.168.2.3<span style="color: #000000;"&gt;

    Subnet Mask . . . . . . . . . . . : </span>255.255.255.0<span style="color: #000000;"&gt;

    Default Gateway . . . . . . . . . : </span>192.168.2.1</pre>

ref:http://www.java-only.com/LoadTutorial.javaonly?id=117

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读