java执行shell命令

阅读更多
			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;
		}
	}

 

你可能感兴趣的:(java,unix)