java 来自http://www.mkyong.com/java/how-to-execute-shell-command-from-java/

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 来自http://www.mkyong.com/java/how-to-execute-shell-command-from-java/相关的知识,希望对你有一定的参考价值。

package com.mkyong.shell;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ExecuteShellComand {

	public static void main(String[] args) {

		ExecuteShellComand obj = new ExecuteShellComand();

		String domainName = "google.com";
		
		//in mac oxs
		String command = "ping -c 3 " + domainName;
		
		//in windows
		//String command = "ping -n 3 " + domainName;
		
		String output = obj.executeCommand(command);

		System.out.println(output);

	}

	private String executeCommand(String command) {

		StringBuffer output = new StringBuffer();

		Process p;
		try {
			p = Runtime.getRuntime().exec(command);
			p.waitFor();
			BufferedReader reader = 
                            new BufferedReader(new InputStreamReader(p.getInputStream()));

                        String line = "";			
			while ((line = reader.readLine())!= null) {
				output.append(line + "\n");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return output.toString();

	}

}

以上是关于java 来自http://www.mkyong.com/java/how-to-execute-shell-command-from-java/的主要内容,如果未能解决你的问题,请参考以下文章

serialVersionUID 序列化

JAXB简单样例

debian9 JDK环境变量设置

mac设置java环境变量, 使用oh-my-zsh

推荐一个找到一个比较好的spring,java学习教程

多对多关系:org.hibernate.MappingException:无法确定类型:java.util.Set