m33ts4k0z
m33ts4k0z

Reputation: 703

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. No errors either. Using the file explorer in Android Studio I can see that the ls is located in that location.

Upvotes: 0

Views: 387

Answers (1)

ddmps
ddmps

Reputation: 4380

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().

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). 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:

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

Upvotes: 1

Related Questions