简体   繁体   中英

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.

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".!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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