1 Star 0 Fork 1

river_rock / ce4j

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

ce4j简介

    基于java.lang.Runtime封装的用于简化java调用命令行的工具。

在使用java原生库的java.lang.Runtime进行命令行调用的过程中,需要重复编写不少代码。 于是基于java.lang.Runtime做了一套封装,封装内部做了输出流的处理,解决管道阻塞等问题,使用者可以忽略输出流的管道阻塞问题,直接进行命令行调用并收集执行输出和结果。此工具可以大大简化java调用命令行的编码。 本着工具的简单化,封装过程没有使用第三方插件,采用策略模式提供命令执行结果的判断自定义,采用builder模式提供Executor的自定义构建。

快速开始

maven依赖

<dependency>
	<groupId>com.github.swjuyhz</groupId>
	<artifactId>ce4j</artifactId>
	<version>1.2</version>
</dependency>

使用demo 示例

#命令行的编写推荐使用命令行构建工具CommandLineBuilder

默认配置Executor

插件默认:使用Stdout输出流,不使用错误输出流,不收集命令行输出,不判定执行结果(即使用空判定策略)

	Executor executor = BaseExecutor.newBuilder().build();
	/*或者(效果同上,此为插件默认配置,完整版):
	Executor executor3 = BaseExecutor
				.newBuilder()
				.useStdoutStreamGobbler(true, CheckStrategy.NONE_CHECK_STRATEGY_INSTANCE)
				.collectAllOutputStdout(false)
				.build();
	*/
	//window 
	//单条命令行
	if(executor.isWin()) {
		ExecutedResult res = executor.execute("cmd /c dir");
		System.out.println(res);
		//多条命令行
		ExecutedResult res1 = executor.execute("cmd /c C: && cd C:\\Users\\zyh\\Desktop\\learning && dir");
		System.out.println(res1);
	}else {
		//Linux
		//单命令行
		ExecutedResult res2 = executor.execute("ls");
		System.out.println(res2);
		//多命令行
		ExecutedResult res3 = executor.executeMutilShell(Arrays.asList("cd /root/deploy/zyh/bin/poems_generator/", "python3 compose_poem.py 佳"));
		System.out.println(res3);
	}

自定义配置Executor

自定义执行结果判定策略、使用流stdoutStream、收集命令行输出

	Executor executor1 = BaseExecutor
				.newBuilder()
				//使用StdoutStream,使用自定义执行结果判定状态,ColonEndStrategy为插件实现的一种场景的判定策略,
            			//自定义判定策略实现com.zyh.ce4j.strategy.CheckStrategy即可
				.useStdoutStreamGobbler(true, new ColonEndStrategy())
				.collectAllOutputStdout(true)//收集命令行输出
				.build();
	//window 
	//单条命令行
	if(executor1.isWin()) {
		ExecutedResult res = executor1.execute("cmd /c dir");
		System.out.println(res);
		Result stdouResult = res.getStdoutResult();
		if(stdouResult.getStatus() == Result.Status.SUCCESS) {
			//收集的命令行执行的STDOUT输出(Runtime.getRuntime().exec(comandLine).getInputStream())
			List<String> lines = stdouResult.getData();
			for(String line:lines) {
				System.out.println(line);
			}
		}
		//多条命令行
		ExecutedResult res1 = executor1.execute("cmd /c C: && cd C:\\Users\\zhengyh\\Desktop\\learning && dir");
		System.out.println(res1);
		Result stdouResult1 = res1.getStdoutResult();
		if(stdouResult1.getStatus() == Result.Status.SUCCESS) {
			//收集的命令行执行的STDOUT输出(Runtime.getRuntime().exec(comandLine).getInputStream())
			List<String> lines = stdouResult1.getData();
			for(String line:lines) {
				System.out.println(line);
			}
		}
	}else {
		//Linux
		//单命令行
		ExecutedResult res2 = executor1.execute("ls");
		System.out.println(res2);
		Result stdouResult = res2.getStdoutResult();
		if(stdouResult.getStatus() == Result.Status.SUCCESS) {
			//收集的命令行执行的STDOUT输出(Runtime.getRuntime().exec(comandLine).getInputStream())
			List<String> lines = stdouResult.getData();
			for(String line:lines) {
				System.out.println(line);
			}
		}
		//多命令行
		ExecutedResult res3 = executor1.executeMutilShell(Arrays.asList("cd /root/deploy/zyh/bin/poems_generator/", "python3 compose_poem.py 佳"));
		System.out.println(res3);
		Result stdouResult3 = res3.getStdoutResult();
		if(stdouResult.getStatus() == Result.Status.SUCCESS) {
			//收集的命令行执行的STDOUT输出(Runtime.getRuntime().exec(comandLine).getInputStream())
			List<String> lines = stdouResult3.getData();
			for(String line:lines) {
				System.out.println(line);
			}
		}
	}

自定义执行结果判定策略、使用errorStream流、收集命令行errorStream流输出

stdoutStream 默认开启,否则可能导致命令行执行堵塞,除非命令行执行后没有该流的输出

	Executor executor2 = BaseExecutor
				.newBuilder()
				//使用错误输出流Runtime.getRuntime().exec(comandLine).getErrorStream()
				.useErrorStreamGobbler(true, new CheckStrategy() {
				    @Override //自定义策略
				    public Result endCheck(String lastPrint) {
				       return new Result(Result.Status.UNKNOWN, "未检查执行结果,执行结果未知。", null);
				    }})
				.collectAllOutputError(true)//收集错误流输出的命令行
				.build();
	//window 
	//单条命令行
	if(executor1.isWin()) {
		ExecutedResult res = executor2.execute("cmd /c dir");
		System.out.println(res);
		Result stdouResult = res.getStdoutResult();
		if(stdouResult.getStatus() == Result.Status.SUCCESS) {
			//收集的命令行执行的STDOUT输出(Runtime.getRuntime().exec(comandLine).getInputStream())
			List<String> lines = stdouResult.getData();
			for(String line:lines) {
				System.out.println(line);
			}
		}
		
		Result errorResult = res.getErrorResult();
		if(stdouResult.getStatus() == Result.Status.UNKNOWN) {//依据自定义的策略
			//收集的命令行执行的ERROR输出(Runtime.getRuntime().exec(comandLine).getErrorStream())
			List<String> lines = errorResult.getData();
			for(String line:lines) {
				System.out.println(line);
			}
		}
		
		
		//多条命令行
		ExecutedResult res1 = executor2.execute("cmd /c C: && cd C:\\Users\\zhengyh\\Desktop\\learning && dir");
		System.out.println(res1);
		Result stdouResult1 = res1.getStdoutResult();
		if(stdouResult1.getStatus() == Result.Status.SUCCESS) {
			//收集的命令行执行的STDOUT输出(Runtime.getRuntime().exec(comandLine).getInputStream())
			List<String> lines = stdouResult1.getData();
			for(String line:lines) {
				System.out.println(line);
			}
		}
		Result errorResult1 = res1.getErrorResult();
		if(stdouResult.getStatus() == Result.Status.UNKNOWN) {//依据自定义的策略
			//收集的命令行执行的ERROR输出(Runtime.getRuntime().exec(comandLine).getErrorStream())
			List<String> lines = errorResult1.getData();
			for(String line:lines) {
				System.out.println(line);
			}
		}
	}else {
		//Linux
		//单命令行
		ExecutedResult res2 = executor2.execute("ls");
		System.out.println(res2);
		Result stdouResult = res2.getStdoutResult();
		if(stdouResult.getStatus() == Result.Status.SUCCESS) {
			//收集的命令行执行的STDOUT输出(Runtime.getRuntime().exec(comandLine).getInputStream())
			List<String> lines = stdouResult.getData();
			for(String line:lines) {
				System.out.println(line);
			}
		}
		Result errorResult1 = res2.getErrorResult();
		if(stdouResult.getStatus() == Result.Status.UNKNOWN) {//依据自定义的策略
			//收集的命令行执行的ERROR输出(Runtime.getRuntime().exec(comandLine).getErrorStream())
			List<String> lines = errorResult1.getData();
			for(String line:lines) {
				System.out.println(line);
			}
		}
		//多命令行
		ExecutedResult res3 = executor2.executeMutilShell(Arrays.asList("cd /root/deploy/zyh/bin/poems_generator/", "python3 compose_poem.py 佳"));
		System.out.println(res3);
		Result stdouResult3 = res3.getStdoutResult();
		if(stdouResult.getStatus() == Result.Status.SUCCESS) {
			//收集的命令行执行的STDOUT输出(Runtime.getRuntime().exec(comandLine).getInputStream())
			List<String> lines = stdouResult3.getData();
			for(String line:lines) {
				System.out.println(line);
			}
		}
		Result errorResult2 = res3.getErrorResult();
		if(stdouResult.getStatus() == Result.Status.UNKNOWN) {//依据自定义的策略
			//收集的命令行执行的ERROR输出(Runtime.getRuntime().exec(comandLine).getErrorStream())
			List<String> lines = errorResult2.getData();
			for(String line:lines) {
				System.out.println(line);
			}
		}
	}
MIT License Copyright (c) 2020 river-rock Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

基于java.lang.Runtime封装的用于简化java调用命令行的工具 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/river_rock/ce4j.git
git@gitee.com:river_rock/ce4j.git
river_rock
ce4j
ce4j
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891