java执行.bat/.sh脚本 实例

private static String exec(String script){
		Runtime r = Runtime.getRuntime();
		InputStream is =null;
		InputStreamReader ir = null;
		BufferedReader br =null;
		String output = "";
		try {
			Process p = r.exec(script);
			is = p.getInputStream();
			ir = new InputStreamReader(is);
			br = new BufferedReader(ir);
			output += br.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if (null != br) {
					br.close();
				}
				if (null != ir) {
					ir.close();
				}
				if (null != is) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return output;
	}

可在脚本中输出相关信息做业务逻辑判断。直接复制粘贴即可。

你可能感兴趣的:(Java)