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. ...
随机推荐
- IPv6 tutorial 3 New features: IPsec and LAN features
https://4sysops.com/archives/ipv6-tutorial-part-3-new-features-ipsec-and-lan-features/ In the last p ...
- mapreduce实现全局排序
直接附代码,说明都在源码里了. package com.hadoop.totalsort; import java.io.IOException; import java.util.ArrayList ...
- hadoop的wordcount例子运行
可以通过一个简单的例子来说明MapReduce到底是什么: 我们要统计一个大文件中的各个单词出现的次数.由于文件太大.我们把这个文件切分成如果小文件,然后安排多个人去统计.这个过程就是”Map”.然后 ...
- 在SQL2005实现维护计划-备份数据库
一.備份數據庫維護計劃方案 [注]: 1.先啟動SQL Server Agent服務 2..交易記錄備份 (只限於完整和大量記錄復原模式). 3.下面中”清除備份trn文件” & “清除備份日 ...
- MFC UpdateData自动更新
嗯,添加一个按钮和我们自己定义的成员变量 关联. 方法1. 在*.rc 或者class 类视图中点击[add variable],就会自动生成DDX_Text(*,*)的内容及其IF 方法2. 自己 ...
- [Jacky] Stoe load reload 区别
load( Object options ) : Boolean 采用配置好的Reader格式去加载Record缓存,具体请求的任务由配置好的Proxy对象完成. reload( Object opt ...
- 1到n数组,和为指定数所有序列问题
(1)方法一,背包问题解法 #include <iostream> using namespace std; #include <vector> #include <li ...
- bzoj 1922 [Sdoi2010]大陆争霸(最短路变形)
Description 在一个遥远的世界里有两个国家:位于大陆西端的杰森国和位于大陆东端的 克里斯国.两个国家的人民分别信仰两个对立的神:杰森国信仰象征黑暗和毁灭 的神曾·布拉泽,而克里斯国信仰象征光 ...
- bzoj2946 [Poi2000]公共串(SA,SAM)
[题意] 多串求LCS. [思路] 主要是想找一下SAM的优越感 :) velui good 后缀数组划分height需要注意不少细节 <_<,然后不停debug [代码] ...
- 微软Azure的多媒体编码服务示例
这篇文章是介绍 Azure 媒体服务编程系列之一.以前的主题是如何: 获得媒体处理器. 为服务器上的媒体内容,可以对内容与大量的媒体编码和格式使用 Azure Media 编码器进行编码.您还可以使用 ...