2

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.

2

1 Answer 1

4

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".!
Sign up to request clarification or add additional context in comments.

3 Comments

,thanks for your help. But now eclipse reports "overview.png: Permission denied". I think I should use command "su root". But I do not how to type in my password in code. Do you know how to solve this problem?
@city: You should just specify filename id directory you can use. For instance: new File("/home/city/overview.png").
@city: alternatively you could run scala from root.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.