0

The code below doesn't work using a Runtime execution in eclipse but the same works using Command Line execution from the root directory of the eclipse project. I have tried different commands such as creating a textfile using notepad which also works using Runtime. But the below command doesn't work. Is there any particular reason for it?

    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec("dot -Tpng output//vowelWithGainRatio.dot > output.png");

output is the directory where the .dot file is present. The command works based on Graphviz Graphics Visualization Software.

Rathan
  • 29
  • 1
  • 8
  • 1
    Can you expand on what "But the below command doesn't work." means? Is there an exception? – bradimus May 11 '17 at 12:46
  • Maybe the 'dot' program is not within the path. Try passing the absolute directory where the program is found. – Robert Kock May 11 '17 at 12:50
  • There is no exception. The console doesn't show any caught exception or any error whatsoever. Statements executing after the above one works as well. Doesn't show up as any caught exceptions or errors. – Rathan May 11 '17 at 12:50
  • @RobertKock `Process pr = rt.exec("C:\\Graphviz2.38\\bin\\dot -Tpng output\\vowelWithGainRatio.dot > output.png");` didn't work as well. – Rathan May 11 '17 at 12:53
  • 1
    Usually, exec() doesn't recognize pipelines. [See here](http://stackoverflow.com/a/31776547/4310386) – Janith Jeewantha May 11 '17 at 13:06
  • @JanithJeewantha Got it, will try it out. Thanks – Rathan May 11 '17 at 13:07

1 Answers1

0

The parameter is not valid for the command Runtime.exec. Also I recommend to use ProcessBuilder instead like following.

Process p = new ProcessBuilder("C:/Graphviz2.38/bin/dot", "-Tpng", "output//vowelWithGainRatio.dot").start();

Then, in order to manage redirection, you need to use ProcessBuilder.redirectOutput() method.

Example :

File output = new File("output.png");
p.redirectOutput(Redirect.appendTo(output));

EDIT

Full example :

Main.java

import java.io.File;
public class Main {
    public static void main(String[] args) {
        ProcessBuilder pb = new ProcessBuilder("echo", "Hello World !");
        File log = new File("log");
        pb.redirectOutput(ProcessBuilder.Redirect.to(log));
        try {
            Process p = pb.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Mickael
  • 4,458
  • 2
  • 28
  • 40
  • `Process pb = new ProcessBuilder("dot", "-Tpng", "output//vowelWithGainRatio.dot").start(); File out = new File("output.png"); pb.redirectOutput(out);` Thanks for your response but I get the error that `redirectOutput is undefined for Process` Could you point out where I went wrong here? – Rathan May 11 '17 at 13:02
  • Thanks for the updated answer but it somehow results in the same i.e no error or exception and executes completely without creating the output file. With the input file as `vowelWithGainRatio.dot` , it should take a few seconds to perform the conversion to png which it doesn't. The statement right after the above code executes. – Rathan May 11 '17 at 13:30
  • I see in your comment that you did `Process pb = new ProcessBuilder("dot", ...` instead of `Process pb = new ProcessBuilder("C:/Graphviz2.38/bin/dot", ...` also if the output file doesn't exist you can use `p.redirectOutput(Redirect.to(output));` – Mickael May 11 '17 at 13:37
  • I've added the location as well* The result is the same unfortunately. – Rathan May 11 '17 at 14:04
  • Could you please activate error redirection with [`ProcessBuilder.redirectError()`](https://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html#redirectError-java.lang.ProcessBuilder.Redirect-) ? Then share the output – Mickael May 11 '17 at 14:25
  • `ProcessBuilder p = new ProcessBuilder("C:\\Graphviz2.38\\bin\\dot", "-Tpng", "C:\\Users\\reva01055\\Desktop\\PO\\MakeUI\\output\\vowelWithGainRatio.dot");` `p.start();` `File output = new File("output.png"); File output2 = new File("output.txt"); p.redirectErrorStream(true); p.redirectOutput(Redirect.appendTo(output)); p.redirectError(Redirect.appendTo(output2));` The above code doesn't create output2.txt although no exceptions and no errors are produced. – Rathan May 11 '17 at 14:40
  • @Rathan what are you doing with your exceptions? You might have to paste your whole code. – matt May 11 '17 at 15:04
  • @matt `try { ProcessBuilder p = new ProcessBuilder("C:\\Graphviz2.38\\bin\\dot", "-Tpng", "C:\\Users\\reva01055\\Desktop\\PO\\MakeUI\\output\\vowelWithGainRatio.dot"); p.start(); File output = new File("output.png"); File output2 = new File("output.txt"); p.redirectErrorStream(true); p.redirectOutput(Redirect.appendTo(output)); p.redirectError(Redirect.appendTo(output2)); } catch(Exception e) { e.printStackTrace(); }` This is the try - catch block. Nothing is printed through the catch block though. – Rathan May 11 '17 at 15:28