Run Graphviz (dot.exe) through Java application

2

I am trying to generate a graph from a .dot file using the Graphviz tool. To do this, within a Java application, I am invoking the command prompt by navigating to the Graphviz installation folder and entering the command as follows:

String cd= "cd C:\Program Files (x86)\Graphviz2.36\bin";  
String comando = " dot -Tpng (...)saidaDot.dot -o (..)out.png ";  
Runtime.getRuntime().exec( "cmd.exe /C start cmd.exe /C "+cd+comando);  

Where (...) symbolizes the files directory.

The command prompt opens and closes quickly and I was not successful in generating the out.png file.

Does anyone have any idea where my error might be?

    
asked by anonymous 25.06.2014 / 23:37

1 answer

1

Always try to encapsulate everything that links to external programs in separate classes.

Your error is that you can not use the cd command when calling the console in java.

I think you're looking for something like this:

import java.io.*;

public class GraphViz {
    public void call(String arquivoDot, String arquivoPNG) {

        try {
            Runtime rt = Runtime.getRuntime();
            String graphDir = "C:\Program Files (x86)\Graphviz2.36\bin\dot.exe -Tpng ";
            String cmdString = graphDir + arquivoDot + " -o " + arquivoPNG;
            System.out.println(cmdString);
            Process pr = rt.exec(cmdString);
            BufferedReader entrada = new BufferedReader(new InputStreamReader(pr.getInputStream()));
            String linha = null;
            while ((linha = entrada.readLine()) != null) {
                System.out.println(line);
            }
            pr.waitFor();
            if(pr.exitValue()!=0)
                System.out.println("Erro de saida " + pr.exitValue());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
    
07.07.2014 / 22:45