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. poj2761Feed the dogs(划分树-区间K值)

    链接 这树着实不好理解啊 讲解http://www.cnblogs.com/pony1993/archive/2012/07/17/2594544.html 对于找K值 右区间的确定不是太理解..先当 ...

  2. hdu4655Cut Pieces

    http://acm.hdu.edu.cn/showproblem.php?pid=4655 先以最大的来算为 N*所有的排列数  再减掉重复的 重复的计算方法:取相邻的两个数的最小值再与它前面的组合 ...

  3. vim解决中文显示乱码问题

    命令:vim ~/.vimrc 写入如下: set enc=utf-8 set fileencoding=utf-8 set fileencodings=ucs-bom,utf8,prc set gu ...

  4. bzoj1267 3784

    双倍经验题像这种方案太多不能全部求出来但求前k大一般有这样一个思路将所有方案无重复不漏的分为若干类,每个类的元素满足单调性,然后我们用堆维护就行了!对于这道题,可以想到用树的分治来处理路径,当处理根为 ...

  5. 阿里云数加平台——BI报表使用概述和总结

    先声明一点,本人写此文章初衷只为对前段时间的工作做些总结,并做个记录,以备日后查用,此外也顺便与他人分享一下.当然间接上也为阿里云的大数据平台做了个免费广告.以下开始正文. 首先进入数加服务的控制面板 ...

  6. zookeeper实现分布锁

    分布式锁服务在大家的项目中或许用的不多,因为大家都把排他放在数据库那一层来挡.当大量的行锁.表锁.事务充斥着数据库的时候.一般web应用很多的瓶颈都在数据库上,这里给大家介绍的是减轻数据库锁负担的一种 ...

  7. Bootstrap基本使用[转]

    Bootstrap是Twitter推出的一个由动态CSS语言Less写成的开源CSS/HTML框架(同时提供Sass 移植版代码).Bootstrap提供了全面的基本及组件样式并自带了13个jQuer ...

  8. 【转】RDO、SAD、SATD、λ

    SAD(Sum of Absolute Difference)=SAE(Sum of Absolute Error)即绝对误差和 SATD(Sum of Absolute Transformed Di ...

  9. leecode 树的平衡判定 java

    以前写过c++版本的,感觉java写的好舒心啊/** * Definition for binary tree * public class TreeNode { * int val; * TreeN ...

  10. oracle 对象上锁,不能插入或删除情况

    ora-00054:resource busy and acquire with nowait specified解决方法 当某个数据库用户在数据库中插入.更新.删除一个表的数据,或者增加一个表的主键 ...