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. java.utils.HashMap数据结构分析(转)

      上图为Hashmap的数据结构图,具体实线是采用数组结合链表实现,链表是为了解决在hash过程中因hash值一样导致的碰撞问题. 所以在使用自定义对象做key的时候,一定要去实现hashcode方 ...

  2. IP隧道基础研究

    static char banner[] __initdata = KERN_INFO "IPv4 over IPv4 tunneling driver\n"; static st ...

  3. wzplayer V1.6正式版(无限制)不支持加密版本 2014-07-08

    2014-07-08 修复部分视频倾斜问题,同时给受影响的客户给予更新. wzplayer V1.6正式版(无限制)不支持加密版本 发布于官方,下载地址wzplayer V1.6正式版(无限制)不支持 ...

  4. vim多标签,多窗口

    多标签 进入vim前 vim -p <文件名> 以多标签形式打开文件.如vim -p * 就是编辑当前目录的所有文件, vim编辑中 :tabnew 增加一个标签 :tabc 关闭当前的t ...

  5. shutdown,init,halt,poweroff,reboot的区别和联系, pkill -kill -t tty7注销

    前言 最近这些天,每天晚上关机前,都会在osc上发一条动弹,“我要init 0了,各位晚安啊”,这是一件再正常不过的事情了. 看似很平常的一件事情,不过在昨晚就被一位同学的回复给难住了,到底是什么样的 ...

  6. bzoj1084

    乍一看这题好难,根本不会: 直到看到1<=m<=2…… 显然分类讨论dp, 很快想到这题的dp和poj2430相类似 m=2的时候f[i,j,k]表示到第i行用了j个矩阵结尾状态为k时最大 ...

  7. 浏览器中的XML与JavaScript

    浏览器中的XML与JavaScript 在处理XML前,你需要在JavaScript中获取它.这一部分展示了一些不同的方法用来在JavaScript中获取XML并且对它进行处理. XML的节点类型 在 ...

  8. cefSharp 设置运行时系统语言

    在使用用CefSharp使用过程中,系统用了很多第三方控件.这些控件很多能够根据浏览器设置的语言来进行控件展示对应语言. 在cefSharp可以设置系统语言,代码如下: CefSharp.Settin ...

  9. Problem - 433C - Codeforces解题报告

    对于这题本人刚开始的时候的想法是:先把最大两数差的位置找到然后merge计算一个值再与一连串相同的数做merge后计算一个值比较取最大值输出:可提交后发现不对,于是本人就搜了一下正解发现原来这题的正确 ...

  10. 解决SQL数据库无法脱机的问题

    数据库无法脱机:原理——有人在占用. 解决办法: select * from master.sys.sysprocesses where dbid=db_id('数据库名称') kill [id]