Search code examples
javagraphstream

GraphStream - No valid display found


I'm following this official tutorial of graphStream. And I'm trying to run this example code taken from there:

import org.graphstream.graph.*;
import org.graphstream.graph.implementations.*;
    
public class Tutorial1 {
    public static void main(String args[]) {
        Graph graph = new SingleGraph("Tutorial 1");
    
        graph.addNode("A");
        graph.addNode("B");
        graph.addNode("C");
        graph.addEdge("AB", "A", "B");
        graph.addEdge("BC", "B", "C");
        graph.addEdge("CA", "C", "A");
    
        graph.display();
    }
}

And I'm getting the following error:

Exception in thread "main" java.lang.RuntimeException: Cannot launch viewer.
    at org.graphstream.graph.implementations.AbstractGraph.display(AbstractGraph.java:212)
    at org.graphstream.graph.implementations.AbstractGraph.display(AbstractGraph.java:204)
    at org.test.Test.main(Test.java:23)
Caused by: org.graphstream.util.MissingDisplayException: No valid display found. Please check your System.setProperty("org.graphstream.ui") statement.
    at org.graphstream.util.Display.getDefault(Display.java:79)
    at org.graphstream.graph.implementations.AbstractGraph.display(AbstractGraph.java:209)
    ... 2 more

I did exactly what the tutorial showed.
what am I missing? what does "No valid display found." means


Solution

  • If you are doing this in a Java application, you need to use version 1.3.

    Add the following dependencies. Version 1.3

    • gs-core
    • gs-algo
    • gs-ui

    Main

    import org.graphstream.graph.*;
    import org.graphstream.graph.implementations.*;
        
    public class Main {
        public static void main(String args[]) {
            Graph graph = new SingleGraph("Tutorial 1");
            graph.setStrict(false);
            graph.setAutoCreate( true );
            graph.addNode("A");
            graph.addNode("B");
            graph.addNode("C");
            graph.addEdge("AB", "A", "B");
            graph.addEdge("BC", "B", "C");
            graph.addEdge("CA", "C", "A");
        
            graph.display();
        }
    }
    

    POM

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>sed.home</groupId>
        <artifactId>JavaTestArea</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>
        <dependencies>
            <dependency>
                <groupId>org.graphstream</groupId>
                <artifactId>gs-core</artifactId>
                <version>1.3</version>
            </dependency>        
            <dependency>
                <groupId>org.graphstream</groupId>
                <artifactId>gs-algo</artifactId>
                <version>1.3</version>
            </dependency>
            <dependency>
                <groupId>org.graphstream</groupId>
                <artifactId>gs-ui</artifactId>
                <version>1.3</version>
            </dependency>
        </dependencies>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>15</maven.compiler.source>
            <maven.compiler.target>15</maven.compiler.target>
        </properties>
    </project>
    

    Output enter image description here

    If you use version 2.0, it needs to be done in a JavaFX, Swing, or Android application.