Using GraphVIZ Native library from Java

I am trying to use graphviz native library from java.I am able to compile the program in Eclipse. But getting exception:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no gv in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1681)
    at java.lang.Runtime.loadLibrary0(Runtime.java:840)
    at java.lang.System.loadLibrary(System.java:1047)
    at org.graphviz.test.Test.<clinit>(Test.java:12)
Could not find the main class: org.graphviz.test.Test. Program will exit.

Here is my code(copied from somewhere of course):

package org.graphviz.test;

import org.graphviz.internal.SWIGTYPE_p_Agedge_t;
import org.graphviz.internal.SWIGTYPE_p_Agnode_t;
import org.graphviz.internal.SWIGTYPE_p_Agraph_t;
import org.graphviz.internal.gv;

    public class Test {
        static {
            System.loadLibrary("gv");
        }

        public static void main(String[] args) {
        SWIGTYPE_p_Agraph_t g, sg;
        SWIGTYPE_p_Agnode_t n, m;
        SWIGTYPE_p_Agedge_t e;

        g = gv.digraph("G");
        System.out.println(gv.setv(g,"aaa","xxx"));
        System.out.println(gv.getv(g,"aaa"));
        sg = gv.graph(g,"SG");
        n = gv.node(g,"hello");
        System.out.println(gv.getv(n,"label"));
        System.out.println(gv.setv(n,"aaa","xxx"));
        System.out.println(gv.getv(n,"aaa"));
        m = gv.node(g,"world");
            System.out.println(gv.getv(m,"aaa"));
        e = gv.edge(n,m);
        System.out.println(gv.setv(e,"aaa","xxx"));
            System.out.println(gv.getv(e,"aaa"));
        gv.rm(e);
        gv.rm(n);
        gv.rm(m);
        gv.rm(g);

        g = gv.readstring("digraph G {a->b}");
        gv.rm(g);

        g = gv.read("hello.gv");
        gv.layout(g,"dot");
        gv.render(g,"png","hello.png");
        gv.rm(g);
        }
        }

I have pointed the library correctly, but at runtime getting UnsatisfiedLinkError. Any one ever tried using the graphviz native library?Please let me know how to configure JNI.

Answers


I believe you're problem is that you are trying to import from gv, but you aren't specifying what you want to import. It's kind of like if you were to just write ---

import org.graphviz.internal;

Instead, try importing a specific library from the gv library. For example, if you want a directed graph, you would probably do something like this ---

import org.graphviz.internal.gv.digraph;

Take a look at the following document and see if it helps at all. It might give you a better idea of where gv comes from and how you can import the methods associated with it.

http://www.graphviz.org/pdf/gv.3java.pdf


Get graphviz-java

Make sure you have graphviz-java installed. For MacPorts this should work like this:

sudo port install graphviz +java

This actually failed for me on the first try and complained about swig-java, this fixed it:

sudo port install swig-java
sudo port install graphviz +java

Now the library is installed under /opt/local/lib/graphviz/java for me, search for libgv.jnilib if you don't find it there.

Setup class path

I compiled in Eclipse, for that I added /opt/local/lib/graphviz/java to the build path as an external class folder.

Compile

This is a simple example that writes an image. Note that System.loadLibrary("gv") must happen before calling anything in graphviz.

import org.graphviz.SWIGTYPE_p_Agedge_t;
import org.graphviz.SWIGTYPE_p_Agnode_t;
import org.graphviz.SWIGTYPE_p_Agraph_t;
import org.graphviz.gv;

public class Main {

    static {
        System.loadLibrary("gv");
    }

    public static void main(String[] args) {
        SWIGTYPE_p_Agraph_t g = gv.digraph("G");
        SWIGTYPE_p_Agnode_t n = gv.node(g, "hello");
        SWIGTYPE_p_Agnode_t m = gv.node(g, "world");
        SWIGTYPE_p_Agedge_t e = gv.edge(n, m);
        gv.layout(g, "dot");
        gv.render(g, "png", "hello.png");
    }

}
Run

Run with java.library.path=/opt/local/lib/graphviz/java, e.g. -Djava.library.path=/opt/local/lib/graphviz/java as VM arguments in an Eclipse run configuration.


Need Your Help

how to check current position in mysql

php

how can i check current number in mysql where....