{"id":19237588,"url":"https://github.com/fengzhizi715/kcommand","last_synced_at":"2026-04-10T10:03:58.054Z","repository":{"id":73171363,"uuid":"183575200","full_name":"fengzhizi715/kcommand","owner":"fengzhizi715","description":"kcommand 是基于 Kotlin 特性实现的执行 Linux/Windows 命令的库","archived":false,"fork":false,"pushed_at":"2025-01-06T13:41:24.000Z","size":239,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-21T06:41:51.982Z","etag":null,"topics":["completablefuture","kotlin-coroutines","rxjava2","rxjava3"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fengzhizi715.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-04-26T06:58:36.000Z","updated_at":"2025-01-06T13:41:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"6fac9252-b231-4362-90e1-635ddb454eca","html_url":"https://github.com/fengzhizi715/kcommand","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/fengzhizi715/kcommand","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fengzhizi715%2Fkcommand","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fengzhizi715%2Fkcommand/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fengzhizi715%2Fkcommand/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fengzhizi715%2Fkcommand/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fengzhizi715","download_url":"https://codeload.github.com/fengzhizi715/kcommand/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fengzhizi715%2Fkcommand/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31637749,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-10T07:40:12.752Z","status":"ssl_error","status_checked_at":"2026-04-10T07:40:11.664Z","response_time":98,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["completablefuture","kotlin-coroutines","rxjava2","rxjava3"],"created_at":"2024-11-09T16:27:31.132Z","updated_at":"2026-04-10T10:03:58.014Z","avatar_url":"https://github.com/fengzhizi715.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# kcommand\n\nkcommand 是基于 Kotlin 特性实现的，执行 Linux/Windows 命令的库\n\n[![@Tony沈哲 on weibo](https://img.shields.io/badge/weibo-%40Tony%E6%B2%88%E5%93%B2-blue.svg)](http://www.weibo.com/fengzhizi715)\n[![License](https://img.shields.io/badge/license-Apache%202-lightgrey.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)\n[![](https://jitpack.io/v/fengzhizi715/kcommand.svg)](https://jitpack.io/#fengzhizi715/kcommand)\n\n# 功能特点：\n\n* 简洁的命令执行方式\n* 支持命令执行的超时机制\n* 对 sudo 命令提供额外的支持\n* 支持函数式\n* 支持 CompletableFuture、RxJava 2、RxJava 3、Kotlin Coroutines\n\n\n# 最新版本\n\n模块|最新版本\n---|:-------------:\nkcommand-core|[![](https://jitpack.io/v/fengzhizi715/kcommand.svg)](https://jitpack.io/#fengzhizi715/kcommand)\nkcommand-rxjava2|[![](https://jitpack.io/v/fengzhizi715/kcommand.svg)](https://jitpack.io/#fengzhizi715/kcommand)\nkcommand-rxjava3|[![](https://jitpack.io/v/fengzhizi715/kcommand.svg)](https://jitpack.io/#fengzhizi715/kcommand)\nkcommand-coroutines|[![](https://jitpack.io/v/fengzhizi715/kcommand.svg)](https://jitpack.io/#fengzhizi715/kcommand)\n\n\n# 下载：\n\n将它添加到项目的 root build.gradle 中：\n\n```groovy\n\tallprojects {\n\t\trepositories {\n\t\t\t...\n\t\t\tmaven { url 'https://jitpack.io' }\n\t\t}\n\t}\n```\n\n```groovy\nimplementation 'com.github.fengzhizi715.kcommand:core:v1.4.0'\n```\n\n```groovy\nimplementation 'com.github.fengzhizi715.kcommand:rxjava2:v1.4.0'\n```\n\n```groovy\nimplementation 'com.github.fengzhizi715.kcommand:rxjava3:v1.4.0'\n```\n\n```groovy\nimplementation 'com.github.fengzhizi715.kcommand:coroutines:v1.4.0'\n```\n\n# kcommand 使用：\n\n### 基本用法\n\n首先，需要通过 CommandBuilder 类构建执行的命令，这些命令支持携带参数。\n\n然后，通过 CommandExecutor.execute() 执行命令。\n\n```kotlin\n    CommandExecutor.execute(CommandBuilder(\"ping\").addArg(\"baidu.com\").build())\n```\n\n更为简洁的写法：\n\n```kotlin\n    CommandExecutor.execute(\"ping baidu.com\")\n```\n\n或者：\n\n```kotlin\n    CommandExecutor.execute{ \"ping baidu.com\" }\n```\n\nCommandExecutor 的 execute() 会返回 ProcessResult 对象。然后通过 ProcessResult 的 getExecutionResult() 获取命令执行的状态。\n\n例如：\n\n```kotlin\n    CommandExecutor.executeCmd {\n        val list = mutableListOf\u003cString\u003e()\n        list.add(\"sh\")\n        list.add(\"-c\")\n\n        val psCommand = \"ps aux | grep java\"\n\n        list.add(psCommand)\n\n        CommandBuilder.buildRawCommand(psCommand, list.toTypedArray())\n    }.getExecutionResult().let {\n        val commandLine = it.command().string()\n        val exitCode = it.exitValue()\n        println(\"command line: $commandLine\\nexecution finished with exit code: $exitCode\\n\\n\")\n    }\n```\n\n### 命令执行的回调\n\n开发者可以使用 Append 在`回调`中获取命令执行的内容。\n\n```kotlin\nfun getPsCmd():Command {\n    val list = mutableListOf\u003cString\u003e()\n    list.add(\"sh\")\n    list.add(\"-c\")\n\n    val psCommand = \"ps aux | grep java\"\n\n    list.add(psCommand)\n\n    return CommandBuilder.buildRawCommand(psCommand, list.toTypedArray())\n}\n\nfun main() {\n\n    val cmd = getPsCmd()\n\n    CommandExecutor.execute(cmd, null, appender = object : Appender {\n\n        override fun appendStdText(text: String) {\n            println(text)\n        }\n\n        override fun appendErrText(text: String) {\n            System.err.println(text)\n        }\n    }).getExecutionResult().let {\n        val commandLine = cmd.string()\n        val exitCode = it.exitValue()\n        println(\"command line: $commandLine\\nexecution finished with exit code: $exitCode\\n\\n\")\n    }\n}\n```\n\n### 使用复合命令\n\n通过使用 CommandBuilder.buildCompositeCommand() 构造所需的复合命令，例如管道命令等\n\n```kotlin\n    val cmd = CommandBuilder.buildCompositeCommand(\"ps aux | grep java\")\n\n    CommandExecutor.execute(cmd).getExecutionResult().let {\n        val commandLine = cmd.string()\n        val exitCode = it.exitValue()\n        println(\"command line: $commandLine\\nexecution finished with exit code: $exitCode\\n\\n\")\n    }\n```\n\n### 使用 sudo\n\n支持 sudo 命令执行一些 Linux 管理员使用的命令。\n\n通过使用 buildSudoCommand() 方法构建 Command，需要传递 Linux 管理员的密码和执行的命令。\n\n```kotlin\n    val cmd = CommandBuilder.buildSudoCommand(\"xxx\",\"dmidecode\")\n\n    CommandExecutor.execute(cmd).getExecutionResult().let {\n        val commandLine = cmd.string()\n        val exitCode = it.exitValue()\n        println(\"command line: $commandLine\\nexecution finished with exit code: $exitCode\\n\\n\")\n    }\n```\n\n### 构建 Windows 命令\n\n通过使用 buildWindowsCommand() 方法构建 Command\n\n```kotlin\n    val cmd = CommandBuilder.buildWindowsCommand(\"dir\")\n```\n\n### 支持 CompletableFuture\n\n通过 ProcessResult 的扩展函数`asCompletableFuture()`等，返回 CompletableFuture 对象\n\n```kotlin\n    val cmd = CommandBuilder.buildCompositeCommand(\"ps aux | grep java\")\n\n    val executionResult = CommandExecutor.execute(cmd).asCompletableFuture().get()\n\n    val commandLine = cmd.string()\n    val exitCode = executionResult.exitValue()\n    println(\"command line: $commandLine\\nexecution finished with exit code: $exitCode\\n\\n\")\n```\n\n### 支持 RxJava \n\n通过 ProcessResult 的扩展函数`asObservable()`等，支持 RxJava2、3\n\n```kotlin\n    val cmd = CommandBuilder.buildCompositeCommand(\"ps aux | grep java\")\n\n    CommandExecutor.execute(cmd)\n        .asObservable()\n        .subscribe {\n\n            val commandLine = cmd.string()\n            val exitCode = it.exitValue()\n            println(\"command line: $commandLine\\nexecution finished with exit code: $exitCode\\n\\n\")\n        }\n```\n\n或者\n\n```kotlin\n    val cmd = CommandBuilder.buildCompositeCommand(\"ps aux | grep java\")\n\n    val sb = StringBuilder()\n\n    CommandExecutor.executeSync(cmd = cmd,appender = object : Appender{\n        override fun appendStdText(text: String) {\n            if (text.contains(cmd.string())) {\n                sb.append(text)\n            }\n        }\n\n        override fun appendErrText(text: String) {\n            TODO(\"Not yet implemented\")\n        }\n\n    }).asObservable {\n        sb.toString()\n    }.subscribe {\n        println(it)\n    }\n```\n\n### 支持函数式\n\n通过 ProcessResult 的`getResult()`返回的 [Result](https://github.com/fengzhizi715/Result) 支持函数式，\n[Result](https://github.com/fengzhizi715/Result) 可以点击查看。\n\n```kotlin\n    val cmd = CommandBuilder.buildCompositeCommand(\"ps aux | grep java\")\n\n    val result = CommandExecutor.execute(cmd).getResult().get()\n\n    if (result is ExecutionResult) {\n\n        val commandLine = cmd.string()\n        val exitCode = result.exitValue()\n        println(\"command line: $commandLine\\nexecution finished with exit code: $exitCode\\n\\n\")\n    }\n```\n\n\n### Kotlin Coroutines\n\n通过 ProcessResult 的扩展函数`asFlow()`，返回 Flow 对象\n\n```kotlin\nfun main() = runBlocking{\n\n    val cmd = CommandBuilder.buildCompositeCommand(\"ps aux | grep java\")\n\n    CommandExecutor.execute(cmd)\n        .asFlow()\n        .collect{\n            val commandLine = cmd.string()\n            val exitCode = it.exitValue()\n            println(\"command line: $commandLine\\nexecution finished with exit code: $exitCode\\n\\n\")\n        }\n}\n```\n\n### 同步执行\n\n使用 CommandExecutor.executeSync() 支持`同步`执行。\n\n\u003e 其实 kcommand 底层使用的是线程池，只是等待线程执行完成后将结果同步返回到 Append 。 \n\nexecuteSync() 方法还支持超时机制，有2个参数分别表示超时的时间、时间的单位。\n\n```kotlin\n    val cmd = CommandBuilder(\"ping\").addArg(\"baidu.com\").build()\n\n    CommandExecutor.executeSync(cmd, null,5, TimeUnit.SECONDS,object :Appender{\n        override fun appendStdText(text: String) {\n            println(text)\n        }\n\n        override fun appendErrText(text: String) {\n            System.err.println(text)\n        }\n\n    }).getExecutionResult().let {\n\n        val commandLine = it.command().string()\n        val exitCode = it.exitValue()\n\n        println(\"command line: $commandLine\\nexecution finished with exit code: $exitCode\\n\\n\")\n    }\n```\n\n### 异步超时\n\n通过 getExecutionResult() 方法进行`异步`超时。\n\n\u003e 该方法在调用 CommandExecutor.executeSync() 会无效。\n\n```kotlin\n    val cmd = CommandBuilder(\"ping\").addArg(\"baidu.com\").build()\n\n    CommandExecutor.execute(cmd, null).getExecutionResult(5,TimeUnit.SECONDS).let {\n\n        val commandLine = it.command().string()\n        val exitCode = it.exitValue()\n\n        println(\"command line: $commandLine\\nexecution finished with exit code: $exitCode\\n\\n\")\n    }\n```\n\n### 同步返回命令的执行结果(String 格式)\n\n通过 getStringWithSync() 方法会同步返回命令的执行结果。\n\n```kotlin\n     val cmd = CommandBuilder.buildCompositeCommand(\"ps aux | grep java\")\n\n     val result = CommandExecutor.getStringWithSync(cmd, appender = object :Appender{\n        override fun appendStdText(text: String) {\n        }\n\n        override fun appendErrText(text: String) {\n            System.err.println(text)\n        }\n    })\n\n    println(result)\n```\n\n\u003e CommandExecutor 有多个类似 getStringWithSync() 的扩展函数。\n\n\nTODO List：\n==\n* 升级 Kotlin 版本\n* 升级 gradle 版本\n\n联系方式\n===\n\nWechat：fengzhizi715\n\n\n\u003e Java与Android技术栈：每周更新推送原创技术文章，欢迎扫描下方的公众号二维码并关注，期待与您的共同成长和进步。\n\n![](https://github.com/fengzhizi715/NetDiscovery/blob/master/images/gzh.jpeg)\n\nLicense\n-------\n\n    Copyright (C) 2018 - present, Tony Shen.\n\n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffengzhizi715%2Fkcommand","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffengzhizi715%2Fkcommand","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffengzhizi715%2Fkcommand/lists"}