Run Graphviz (dot.exe) through Java application

Asked

Viewed 406 times

2

I am trying to generate a graph from a . dot file using the Graphviz tool. To do so, inside a Java application, I am invoking the command prompt, navigating to the Graphviz installation folder and inserting 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 file directory.

Command prompt opens and closes quickly and I was unsuccessful in file generation out png..

Does anyone have any idea where my mistake might be?

1 answer

1


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

Your mistake is that you cannot use the command cd when calling the console in java.

I believe you’re looking for something this way:

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();
        }

    }
}
  • Thank you so much for the @Kyllopardiun reply, now it worked. I just had to make a change to the line: String cmdString = graphDir + arquivoDot + " -o " + arquivoPNG; Now it worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.