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

package com.javaonly.system;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; public class SystemCommandsTest { public static void main(String args[]) {
StringBuffer output = new StringBuffer();
try {
Process process = Runtime.getRuntime().exec(
"ipconfig");
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream())); String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
System.out.println("OUTPUT:"+output.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
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

OUTPUT:

Windows IP Configuration

Ethernet adapter Local Area Connection 2:

        Connection-specific DNS Suffix  . : 

        IP Address. . . . . . . . . . . . : 192.168.2.3

        Subnet Mask . . . . . . . . . . . : 255.255.255.0

        Default Gateway . . . . . . . . . : 192.168.2.1

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

Executing System commands in Java---ref的更多相关文章

  1. java.lang.System.arraycopy() 与java.util.Arrays.copyOf()的区别

    java.lang.System.arraycopy() 与java.util.Arrays.copyOf()的区别 一.java.lang.System.arraycopy() 该方法的声明: /* ...

  2. Sublime Text Build System——编译运行Java

    今天Google如何在ST中编译运行Java的时候,无意中发现了一个更好的方法. 其实,在ST中是可以编译Java的,但是运行不了,因为没有配置运行命令.那么一般的配置方法都是如下的: http:// ...

  3. Exception from System.loadLibrary(smjavaagentapi) java.lang.UnsatisfiedLinkError: no smjavaagentapi in java.library.path

    可能原因: 缺少smjavaagentapi.jar文件或者libsjavaagentapi.so缺少相关的依赖包. 解决方法: 1. 检查sso的lib下面是否有smjavaagentapi.jar ...

  4. JRE System Library 与Java EE Libraries的区别

    JRE System Library是只要做java开发都需要的完整的.标准的库.  Java EE5 Libraries只是java三个方向中做java EE所需要的库.如果做Web方面的开发的话就 ...

  5. Operating System Concepts with java 项目: Shell Unix 和历史特点

    线程间通信,fork(),waitpid(),signal,捕捉信号,用c执行shell命令,共享内存,mmap 实验要求: 1.简单shell: 通过c实现基本的命令行shell操作,实现两个函数, ...

  6. Oracle Fusion Middleware Supported System check,jdk,java .etc requirements

    http://www.oracle.com/technetwork/middleware/ias/downloads/fusion-certification-100350.html 在oracle官 ...

  7. SQL注入备忘单

    Find and exploit SQL Injections with free Netsparker SQL Injection Scanner SQL Injection Cheat Sheet ...

  8. 实战Java虚拟机之四:提升性能,禁用System.gc() ?

    今天开始实战Java虚拟机之四:"禁用System.gc()". 总计有5个系列 实战Java虚拟机之一“堆溢出处理” 实战Java虚拟机之二“虚拟机的工作模式” 实战Java虚拟 ...

  9. java System.getProperty()参数大全

    java.version Java Runtime Environment versionjava.vendor Java Runtime Environment vendorjava.vendor. ...

随机推荐

  1. Android ServiceConnection

    绑定到一个Service 应用组件(客户端)可以调用bindService()绑定到一个service.Android系统之后调用service的onBind()方法,它返回一个用来与service交 ...

  2. Oracle系列之函数

    涉及到表的处理请参看原表结构与数据  Oracle建表插数据等等 如何调用该过程 call function_name(参数值1,参数值2); 创建function来查询某个雇员的工资 create ...

  3. Atom 扩展离线安装

    1.下载原始包 2.解压放入atom的packages文件夹中 3.通过nodejs的npm指令进行安装 运行->cmd 4.重启Atom就ok了

  4. Understanding Memory Management(2)

    Understanding Memory Management Memory management is the process of allocating new objects and remov ...

  5. phpstrom 与 xdebug 配合实现PHP单步调试

    不说废话,直接开始. 第一步: 安装并配置xdebug 安装 可以从官网直接下载对应php版本的xdebug,下载地址:  https://xdebug.org/download.php 配置,典型的 ...

  6. bzoj1007

    其实吧,就是一个半平面交,而且不用考虑转回来的情况,所以只要极角排序然后用栈即可给的是点斜式,比极角很方便至于完整版的半平面交还没写过,看到再说吧 ..] of longint; v:..] of b ...

  7. Guid 的几种形式

    Guid.NewGuid().ToString()得几种格式显示 1.Guid.NewGuid().ToString("N") 结果为:       38bddf48f43c485 ...

  8. ruby编程语言-学习笔记1

    安装完 ruby ri irb ruby-devel 1. 先来个简单的,写个helloworld  给新手们 (terminal中,# 代表root权限,$ 代表用户权限, 前面的就不写了.) # ...

  9. POJ 2502 Subway dij

    这个题的输入输出注意一下就好 #include<cstdio> #include<cstring> #include<queue> #include<cstd ...

  10. Codeforces Round #343 (Div. 2) A. Far Relative’s Birthday Cake

    水题 #include<iostream> #include<string> #include<algorithm> #include<cstdlib> ...