Executing System commands in Java---ref
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的更多相关文章
- java.lang.System.arraycopy() 与java.util.Arrays.copyOf()的区别
java.lang.System.arraycopy() 与java.util.Arrays.copyOf()的区别 一.java.lang.System.arraycopy() 该方法的声明: /* ...
- Sublime Text Build System——编译运行Java
今天Google如何在ST中编译运行Java的时候,无意中发现了一个更好的方法. 其实,在ST中是可以编译Java的,但是运行不了,因为没有配置运行命令.那么一般的配置方法都是如下的: http:// ...
- Exception from System.loadLibrary(smjavaagentapi) java.lang.UnsatisfiedLinkError: no smjavaagentapi in java.library.path
可能原因: 缺少smjavaagentapi.jar文件或者libsjavaagentapi.so缺少相关的依赖包. 解决方法: 1. 检查sso的lib下面是否有smjavaagentapi.jar ...
- JRE System Library 与Java EE Libraries的区别
JRE System Library是只要做java开发都需要的完整的.标准的库. Java EE5 Libraries只是java三个方向中做java EE所需要的库.如果做Web方面的开发的话就 ...
- Operating System Concepts with java 项目: Shell Unix 和历史特点
线程间通信,fork(),waitpid(),signal,捕捉信号,用c执行shell命令,共享内存,mmap 实验要求: 1.简单shell: 通过c实现基本的命令行shell操作,实现两个函数, ...
- Oracle Fusion Middleware Supported System check,jdk,java .etc requirements
http://www.oracle.com/technetwork/middleware/ias/downloads/fusion-certification-100350.html 在oracle官 ...
- SQL注入备忘单
Find and exploit SQL Injections with free Netsparker SQL Injection Scanner SQL Injection Cheat Sheet ...
- 实战Java虚拟机之四:提升性能,禁用System.gc() ?
今天开始实战Java虚拟机之四:"禁用System.gc()". 总计有5个系列 实战Java虚拟机之一“堆溢出处理” 实战Java虚拟机之二“虚拟机的工作模式” 实战Java虚拟 ...
- java System.getProperty()参数大全
java.version Java Runtime Environment versionjava.vendor Java Runtime Environment vendorjava.vendor. ...
随机推荐
- Using innodb_large_prefix to avoid ERROR #1071,Specified key was too long; max key length is 1000 bytes
Using innodb_large_prefix to avoid ERROR 1071 单列索引限制上面有提到单列索引限制767,起因是256×3-1.这个3是字符最大占用空间(ut ...
- openwrt开发
之前写过一篇日志,是关于如何搭建自己的OpenWRT开发环境.经过最近一段时间的开发学习和实践,对OpenWRT环境的开发有了一定的了解.在这里将我的开发心得做个整理. 1.搭建开发环境 首先,我们需 ...
- apache开源项目--Apache POI
Apache POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目.目前POI已经有了Ruby版本. 结构: HSSF - 提供读写Microsoft Excel XLS格式 ...
- addChildViewController 与 addSubview
在viewcontrollerA中, 如果想把controllerB.view添加进来, 可以用 addSubview, 但如果controllerB中有个事件, 使用到 self.navigatio ...
- (转载)file_get_contents("php://input")
(转载)http://taoshi.blog.51cto.com/1724747/1165499 $data = file_get_contents("php://input"); ...
- 使--no-ri --no-rdoc成为gem安装的默认选项
在使用gem install命令的时候,希望加上--no-ri --no-rdoc选项,但是不希望每一次都手动加上这个选项. 其实可以通过编辑配置文件,改变gem install的默认选项. 在win ...
- 用生活例子来解释Java synchronized块
今天满世界的微信小程序的新闻,大家都说对于Android原生程序有构成危险了,我也不想了,以后的事谁知道呢, 我还是好好执行一下今年的计划吧. 项目刚刚上线,最近没啥事,我一直感觉自己的Java基础 ...
- aggregate 和 treeAggregate 的对比
1.定义 [aggregate] /** * Aggregate the elements of each partition, and then the results for all the pa ...
- PHP获取上个月、下个月、本月的日期
获取本月日期: 代码如下: function getMonth($date){ $firstday = date("Y-m-01",strtotime($date)); $la ...
- bzoj 3172 [Tjoi2013]单词(fail树,DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3172 [题意] 题目的意思是这样的,给若干个单词,求每个单词在这一堆单词中的出现次数. ...