简体   繁体   English

在Android上运行Shell命令不会返回任何内容

[英]Running a shell command on Android returns nothing

I've been trying to get the output of a command for quite some hours now but with no result. 我已经尝试获取命令的输出已经有好几个小时了,但没有结果。 Having this code: 具有以下代码:

val byteArry = ByteArray(1024)
 var Holder: String = ""
 try {
      val processBuilder = ProcessBuilder("/system/bin/ls")
      val process = processBuilder.start()
      val  inputStream = process.getInputStream()
      while (inputStream.read(byteArry) !== -1) {
            Holder += String(byteArry)
      }
      inputStream.close()
      } catch (ex: IOException) {
        ex.printStackTrace()
      }
      println("Output: " + Holder)

I'm trying with different (kotlin and java) ways to receive some output from any command but I receive nothing. 我正在尝试以不同的方式(kotlin和Java)从任何命令接收一些输出,但是我什么也没收到。 No errors either. 也没有错误。 Using the file explorer in Android Studio I can see that the ls is located in that location. 使用Android Studio中的文件资源管理器,我可以看到ls位于该位置。

I'm guessing that there's an error happening. 我猜测发生了错误。 You can check for that by either setting ProcessBuilder.redirectErrorStream (before start() ) to get it to the given inputStream or simply getting a separate InputStream for errors by calling Process.getErrorStream() . 您可以通过设置ProcessBuilder.redirectErrorStream (在start()之前)将其获取到给定的inputStream或通过调用Process.getErrorStream()来获取错误的单独InputStream来进行检查。

It's not clear what you're trying to achieve, so it's hard to give you a definitive answer. 目前尚不清楚您要实现的目标,因此很难给出确切的答案。 Your current code is just trying to run a process that is located at /system/bin/ls (from working directory). 您当前的代码只是试图运行位于/system/bin/ls (来自工作目录)的进程。 If you're in fact trying to run ls for directory /system/bin/ , you're going to want to send the directory as an argument to ProcessBuilder , as so: 如果实际上您试图为目录/system/bin/运行ls ,那么您将要将该目录作为参数发送给ProcessBuilder ,如下所示:

  val processBuilder = ProcessBuilder("ls", "/system/bin/")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM