Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6009899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:02:16+00:00 2026-05-23T02:02:16+00:00

In Python, if I want to call an external command as a subprocess, I

  • 0

In Python, if I want to call an external command as a subprocess, I do the following:

from subprocess import Popen, PIPE
cmd = ['cat', '-be']
out, err = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate("some input")

What’s the standard way to do the same in Scala? Using Java’s ProcessBuilder I came up with the following, but it’s pretty ugly:

def communicate(cmd: List[String], input: Option[String] = None): (String, String) = {

    val command = new java.util.ArrayList[String]()
    cmd.foreach(command.add(_))

    val builder = new ProcessBuilder(command)
    val process = builder.start()

    val stdinWriter = new java.io.PrintWriter((new java.io.OutputStreamWriter(new java.io.BufferedOutputStream(process.getOutputStream()))), true);
    val stdoutReader = new java.io.BufferedReader(new java.io.InputStreamReader(process.getInputStream()))
    val stderrReader = new java.io.BufferedReader(new java.io.InputStreamReader(process.getErrorStream()))

    input.foreach(stdinWriter.write(_))
    stdinWriter.close()

    def read(reader: java.io.BufferedReader): String = {
        val out = new ListBuffer[String]
        var line: String = reader.readLine()
        while (line != null) {
            out += line
            line = reader.readLine()
        }
        return out.result.mkString("\n")
    }

    val stdout = read(stdoutReader)
    val stderr = read(stderrReader)

    stdoutReader.close()
    stderrReader.close()

    return (stdout, stderr)

}

val (catout, caterr) = communicate(List("cat", "-be"), Some("some input"))
val (pwdout, pwderr) = communicate(List("pwd"))

Is there a better alternative built into Scala already?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:02:17+00:00Added an answer on May 23, 2026 at 2:02 am

    The answer here How does the “scala.sys.process” from Scala 2.9 work? shows how to use the new Scala 2.9 scala.sys.process.Process. If you don’t use 2.9 you can use the process part of sbt; which the Scala process originates from.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to call an external program from Python. I have used both Popen()
I want to call an external program from within python, but this external program
I want to call a Fortran program from python. I use the Popen statement
I want to execute python code from C# with following code. static void Main(string[]
I want to call a C library from a Python application. I don't want
I want to call a exe from python on a 64bit version of vista.
So, I want to call a Python callback function from C. At some point,
I want to call functions of a C++ program from Python code. How can
I want to call msvcrt functions from 64-bit python using the ctypes package. I'm
I want my Python script to be able to read Unicode command line arguments

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.