java执行shell命令_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java执行shell命令

java执行shell命令

 2014/4/9 12:40:22  lvmlvy  程序员俱乐部  我要评论(0)
  • 摘要:bDelOldFolder=ShellCmdUtils.callShell("rm-rf/usr/ne/code/");StringBuildersb=newStringBuilder("cd/usr/ne/code/");sb.append(task.getNeName()).append(";").append("chmod-R755*;").append("dos2unixbuild_rpm.sh;").append("./build_rpm.sh;");String[]command=
  • 标签:Java 执行 命令
class="java">			bDelOldFolder = ShellCmdUtils
					.callShell("rm -rf /usr/ne/code/");

			StringBuilder sb = new StringBuilder("cd /usr/ne/code/");
			sb.append(task.getNeName())
					.append(";")
					.append("chmod -R 755 *;")
					.append("dos2unix build_rpm.sh;")
					.append("./build_rpm.sh;");
			String[] command = { "/bin/sh", "-c", sb.toString() };
			boolean bBuild = ShellCmdUtils.callShell(command);


	public static boolean callShell(String shellString) {
		try {
			Process process = Runtime.getRuntime().exec(shellString);
			BufferedReader br = new BufferedReader(new InputStreamReader(
					process.getInputStream()));
			String line = new String();
			while ((line = br.readLine()) != null) {
				LOG.info(line);
			}
			int exitValue = process.waitFor();
			if (0 != exitValue) {
				LOG.error("call shell failed. error code is :" + exitValue);
				return false;
			}
			br.close();
			return true;
		} catch (Exception e) {
			LOG.error("call shell failed. " + e);
			return false;
		}
	}
	
	public static boolean callShell(String[] shellStrings) {
		try {
//			String[] cmd = { "/bin/sh", "-c", shellString };
			Process process = Runtime.getRuntime().exec(shellStrings);
			BufferedReader br = new BufferedReader(new InputStreamReader(
					process.getInputStream()));
			String line = new String();
			while ((line = br.readLine()) != null) {
				LOG.info(line);
			}
			int exitValue = process.waitFor();
			if (0 != exitValue) {
				LOG.error("call shell failed. error code is :" + exitValue);
				return false;
			}
			br.close();
			return true;
		} catch (Exception e) {
			LOG.error("call shell failed. " + e);
			return false;
		}
	}

?

发表评论
用户名: 匿名