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

时间: 2024-10-07 09:30:13

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() 该方法的声明: /* @param src 源数组 * @param srcPos 源数组中的起始位置 * @param dest 目标数组 * @param destPos 目标数组中的起始位置 * @param length 需要被复制的元素个数 * @exception IndexOutOfBoundsExcep

Xamarin System.Exception与Java.Lang.Exception之间需要主要的地方

转载请注明出处:http://blog.csdn.net/supluo/article/details/42101569 Xamarin可以用c#来开发Android应用程序,这个就不用多说了. 说说用c#来开发android和用Java来开发android两者之间在异常处理上面需要注意的地方: 见以下简短的代码: button.Click += delegate { try { throw new System.Exception("抛出SystemException"); } ca

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

线程间通信,fork(),waitpid(),signal,捕捉信号,用c执行shell命令,共享内存,mmap 实验要求: 1.简单shell: 通过c实现基本的命令行shell操作,实现两个函数,main()和setup(). setup读取用户的下一条指令(最多80个字符),然后分解为独立的标记,并执行,用户按ctrl+D后,程序终止.     Main函数打印提示符COMMAND->,等待用户输入命令,如果用户命令以” &”结尾,则并发执行,否则,父进程需等待子进程 2.创建历史特性

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

http://www.oracle.com/technetwork/middleware/ias/downloads/fusion-certification-100350.html 在oracle官方网站默认下载的jdk是最新的,目前正式版1.8beta版也已经放出. 但有些项目要求是1.6的jdk,费了九牛二虎之力终于找到了1.6的官方版本,链接如下: http://www.oracle.com/technetwork/java/javase/archive-139210.htmlhttp:

os.system commands subprocess

用来执行shell指令 1.os.system() system(command) -> exit_status Execute the command (a string) in a subshell. import os result = os.system('ls /') print(result) print(type(result)) 输出: bin boot data dev etc home lib lib64 lost+found media mnt opt proc root

SQL注入备忘单

Find and exploit SQL Injections with free Netsparker SQL Injection Scanner SQL Injection Cheat Sheet, Document Version 1.4 About SQL Injection Cheat Sheet Currently only for MySQL and Microsoft SQL Server, some ORACLE and some PostgreSQL. Most of sam

【java错误】System.out.println()出错

今天想测试java的System的类,没想到居然出错了.在同一个包下的java文件System全错,而其他包中的System没错.上网查了下资料,原来我是重定义了System类,覆盖了原来的System类导致原System类中的属性和方法不能使用. 从图中可以看到我定义的公有类是System,覆盖原来的System类. 解决方法也很简单,重新创建一个不以System为名的java文件即可. ...ok了.

java中的System.getProperty()和getenv()方法

Java system.getproperty(String name)获取系统属性 属性说明: public class test{ public static void main(String[] args) { System.out.println("Java Runtime Environment version = " + System.getProperty("java.version")); System.out.println("Java

java的System.getProperty()方法可以获取的值

java.version Java 运行时环境版本 java.vendor Java 运行时环境供应商 java.vendor.url Java 供应商的 URL java.home Java 安装目录 java.vm.specification.version Java 虚拟机规范版本 java.vm.specification.vendor Java 虚拟机规范供应商 java.vm.specification.name Java 虚拟机规范名称 java.vm.version Java 虚