java执行shell命令

5 篇文章 0 订阅
2 篇文章 0 订阅
			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执行Shell脚本可以使用`Runtime`类或`ProcessBuilder`类。下面是使用`Runtime`类执行Shell脚本的示例代码: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ShellScriptExecutor { public static void main(String[] args) { try { // 执行Shell脚本命令 String command = "sh /path/to/script.sh"; Process process = Runtime.getRuntime().exec(command); // 获取Shell脚本输出结果 BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } // 等待Shell脚本执行完成 int exitCode = process.waitFor(); System.out.println("Shell脚本执行完成,退出码:" + exitCode); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } ``` 上述代码中,`/path/to/script.sh`是你要执行Shell脚本的路径。你可以将实际的Shell脚本路径替换到代码中。 使用`ProcessBuilder`类执行Shell脚本的示例代码如下: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ShellScriptExecutor { public static void main(String[] args) { try { // 执行Shell脚本命令 ProcessBuilder processBuilder = new ProcessBuilder("sh", "/path/to/script.sh"); Process process = processBuilder.start(); // 获取Shell脚本输出结果 BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } // 等待Shell脚本执行完成 int exitCode = process.waitFor(); System.out.println("Shell脚本执行完成,退出码:" + exitCode); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } ``` 同样,你需要将实际的Shell脚本路径替换到代码中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值