All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.webapp.utils.file.CmdUtils Maven / Gradle / Ivy

The newest version!
package com.webapp.utils.file;

import java.io.IOException;
import java.util.Arrays;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.webapp.utils.config.SysUtils;
import com.webapp.utils.string.Utils;

public final class CmdUtils {

	private static final Logger logger = LoggerFactory.getLogger(CmdUtils.class);

	public static String execResult(String cmd) {
		return execResult(cmd.split(" "));
	}
	public static String execResult(String cmd, String encoding) {
		return execResult(cmd.split(" "), encoding);
	}

	public static String execResult(String[] cmd) {
		return execResult(cmd, SysUtils.isLinux() ? "utf-8" : "gbk");
	}
	public static String execResult(String[] cmd, String encoding) {
		String result = null;
		try {
			ProcessBuilder builder = new ProcessBuilder(cmd);
			Process exec = builder.start();
			result = IOUtils.toString(exec.getInputStream(), encoding);
		} catch (IOException e) {
			logger.error("Execute command " + Utils.split(Arrays.asList(cmd), " "), e);
		}
		return result;
	}

	public static void exec(String cmd) {
		exec(cmd.split(" "));
	}
	public static void exec(String[] cmd) {
		try {
			new ProcessBuilder(cmd).start();
		} catch (IOException e) {
			logger.error("Execute command " + Utils.split(Arrays.asList(cmd), " ") , e);
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy