city
city

Reputation: 2154

how to execute a command in scala?

I want to execute this command "dot -Tpng overview.dot > overview.png ", which is used to generate an image by Graphviz.

The code in scala:

Process(Seq("dot -Tpng overview.dot > overview.png"))

It does not work. And I also want to open this image in scala. I work under Ubuntu. By default, images will be opened by image viewer. But I type "eog overview.png" in terminal, it reports error

** (eog:18371): WARNING **: The connection is closed

Thus, I do not know how to let scala open this image.

Thanks in advance.

Upvotes: 2

Views: 232

Answers (1)

senia
senia

Reputation: 38045

You can't redirect stdout using > in command string. You should use #> and #| operators. See examples in process package documentation.

This writes test into test.txt:

import scala.sys.process._
import java.io.File

// use scala.bat instead of scala on Windows
val cmd = Seq("scala", "-e", """println(\"test\")""") #> new File("test.txt")
cmd.!

In your case:

val cmd = "dot -Tpng overview.dot" #> new File("overview.png")
cmd.!

Or just this (since dot accepts output file name as -ooutfile):

"dot -Tpng overview.dot -ooverview.png".!

Upvotes: 4

Related Questions