score:1
Accepted answer
You can take advantage of the fact that Scala's ProcessBuilder
methods also accept Seq[String]
.
import sys.process._
object SO {
def runCommand(cmd: String*): (Int, String, String) = {
val stdoutStream = new java.io.ByteArrayOutputStream
val stderrStream = new java.io.ByteArrayOutputStream
val stdoutWriter = new java.io.PrintWriter(stdoutStream)
val stderrWriter = new java.io.PrintWriter(stderrStream)
val exitValue =
cmd.!(ProcessLogger(stdoutWriter.println, stderrWriter.println))
stdoutWriter.close()
stderrWriter.close()
(exitValue, stdoutStream.toString, stderrStream.toString)
}
def main(args: Array[String]): Unit = {
val code =
"""
|import sys
|
|print('1')
|print("2")
|print(sys.argv[1])
""".stripMargin
val arg = 3
println(runCommand("python3","-c", code, arg.toString))
}
}
Source: stackoverflow.com
Related Query
- How to run python code with args with python -c command from scala
- how to run export command from Scala code to add new environment variable on Linux?
- How can I debug why a sys.process command from scala is failing with bad exit code (3) when it works from the command line?
- How to run tests on every code change in IntelliJ IDEA from Scala sbt project?
- How to run JavaScript code from within Scala (JVM)?
- How do I run Scala + specs2 from the command line?
- How to run Scala 3 applications in the command line with Coursier
- How o run a NetBeans-built Scala application jar from command line outside IDE?
- How to run scala file from command line?
- "Stream Processing with Apache Flink" how to run book code from IntelliJ?
- How to run Scala compiled code in command line?
- Can I run JUnit 4 to test Scala code from the command line?
- How to run a Scala class that's in a package from the command line
- Run scala unit test from command line separately with Maven
- How to run scala code from terminal
- how to avaoid circular dependency exception in spark scala and make code run with cyclic dependency
- How to compile and run scala code at runtime from a java program?
- how to fill case class with args from command line?
- scala spark sql with intellij, how to link jdbc jar file and run the scala code
- How to build executable jar from snowpark scala maven application and run from command line
- How to change the code from Scala to Python
- Spark: How to map Python with Scala or Java User Defined Functions?
- How to run scala code on Intellij Idea 11?
- How to use Scala varargs from Java code
- reader writer state monad - how to run this scala code
- How to run an external file from within the scala interactive interpreter (REPL)?
- Scala - How to compile code from an external file at runtime?
- How to run Scala script using spark-submit (similarly to Python script)?
- How to run Scala code in Intellij Idea 10
- How to start a Scala method from command line?
More Query from same tag
- Sorted table with a map in Apache Ignite
- Expose private case class in Scala
- Firebase Java SDK doesn't persist data and doesn't trigger callback. Tasks seems never complete
- How can I differentiate between def foo[A](xs: A*) and def foo[A, B](xs: (A, B)*)?
- Functional style for this Scala code
- Equivalence of complicated for and if condition in C++ to Scala
- analogous Try block to try/finally block in scala
- Registering serializers in Storm config
- sbt multi project undefined settings
- Is there an equivalent to Colander/DictShield for Java/Scala?
- Does Scala have a value restriction like ML, if not then why?
- Handle Java method 'paused()' in Scala?
- Scala XML Library for 2.10+
- How to depend on a setting in the "current" config
- How to write this three-liner as a one-liner?
- Websocket with Graph DSL
- Spark from_json No Exception
- How to convert a case Class to Json-LD in Scala?
- Mixing dynamic objects with structural typing in Scala
- Does the incremental compilation speed in Scala depend on the number of classes per file?
- Akka. Is there a way to share code between two receives?
- Last unique entries above current row in Spark
- Dataflow with Cloud Spanner: java.lang.IllegalArgumentException: Jetty ALPN/NPN has not been properly configured
- Parse Numeric OR String value from Json response - Scala - Play
- How to fix [ERROR] i.g.a.Gatling$ - Run crashed java.lang.IllegalArgumentException: requirement failed: No scenario set up?
- Why warning when scala.language.implicitConversions is not the last import?
- Consume all of a Kafka topic and then immediately disconnect?
- Running Scala module with databricks-connect
- Scala traits with generic
- Interleaving iterators