Using JavaGD in Java applications

Goal: Create a Java application with a JFrame that contains a JGDPanel from JavaGD and plot something via JRI.

Solution: The following code is a derivate of JGR code snipplets under GPL-2:

import java.awt.Component;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;

import org.rosuda.JRI.Rengine;
import org.rosuda.javaGD.GDInterface;
import org.rosuda.javaGD.JGDBufferedPanel;

public class JavaGDExample extends GDInterface implements WindowListener {

    JFrame f;
   
    public void gdOpen(double w, double h) {
        if (f!=null) gdClose();
        f = new JFrame("JavaGD Example");
        f.addWindowListener(this);      
        c = new JGDBufferedPanel(w, h);
        f.getContentPane().add((Component) c);
        f.pack();
        f.setVisible(true);
    }
   
    public void gdClose() {
        super.gdClose();
        if (f!=null) {
            c=null;
            f.removeAll();
            f.dispose();
            f=null;
        }
    }

    public static void main(String[] args) {
        Rengine engine = new Rengine(new String[] {"--vanilla"}, true, null);      
        engine.eval(".setenv <- if (exists("Sys.setenv")) Sys.setenv else Sys.putenv");
        engine.eval(".setenv("JAVAGD_CLASS_NAME"="JavaGDExample")");
        engine.eval("library(JavaGD)");
        engine.eval("JavaGD()");
        engine.eval("plot(rnorm(100))");       
    }
   
    /** listener response to "Close" - effectively invokes <code>dev.off()</code> on the device */
    public void windowClosing(WindowEvent e) {
        if (c!=null) executeDevOff();
    }
    public void windowClosed(WindowEvent e) {}
    public void windowOpened(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
}

Et voilà:

JavaGDExample

Of course you have to make sure that the JRI native library is in a directory listed in java.library.path, for example on my machine via:

java -Djava.library.path=/usr/local/lib/R/site-library/rJava/jri/ JavaGDExample

Also you have to provide the correct path for the environment variable R_HOME such as:

R_HOME=/usr/lib64/R/

And of course you need the two jars javaGD.jar and JRIEngine.jar in your CLASSPATH.

8 thoughts on “Using JavaGD in Java applications

  1. Michelle

    Hi there,

    Thanks for the helpful post. When I run your example, I get this error:

    Exception in thread “Thread-1” java.lang.NoClassDefFoundError: JavaGDExample
    at org.rosuda.JRI.Rengine.rniEval(Native Method)
    at org.rosuda.JRI.Rengine.eval(Rengine.java:549)
    at org.rosuda.JRI.Rengine.eval(Rengine.java:527)
    at gov.pnl.mpv.views.JavaGDExample.main(JavaGDExample.java:42)
    Caused by: java.lang.ClassNotFoundException: JavaGDExample
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    … 4 more

    Do you have any suggestions?

    Reply
  2. Kornelius Rohmeyer Post author

    Dear Michelle, please try to add the directory where the file JavaGDExample.class is found to the CLASSPATH. If you call java directly in the directory, it is simply the following in the bash on unix machines:
    export CLASSPATH=$CLASSPATH:.

    Reply
  3. Michael Theobald

    Eliminating the import statements for the java.awt and javax.swing classes as well as the doOpen() and doClose() methods and the WindowListener code makes no difference in the function of this class as published. Only the code in the main() method appears to be used. Therefore, in my opinion, the code example could either be made more complete or made shorter.

    It does work!
    Michael

    Reply
  4. Kornelius Rohmeyer Post author

    Hi Michael,
    yes of course the extensibility is the goal! Of course you can just write a main method with the following lines and nothing else in the class:

    Rengine engine = new Rengine(new String[] {"--vanilla"}, true, null);      
    engine.eval("library(JavaGD)");
    engine.eval("JavaGD()");
    engine.eval("plot(rnorm(100))");

    But that’s not the point.

    Best regards, Kornelius.

    Reply
  5. Zoe

    Thanks for the helpful post.
    I am not very familar with R. I have just tried your example, it doesn’t work on my PC (Windows). I am wondering if it is because of the way to environment variable in Windows should be different than the code in your example as following:
    engine.eval(“.setenv <- if (exists("Sys.setenv")) Sys.setenv else Sys.putenv");
    engine.eval(".setenv("JAVAGD_CLASS_NAME"="JavaGDExample")");

    How could I set the environment variable in PC?

    Thanks a lot,
    Zoe

    Reply
  6. Deepak

    Hi,
    Could run the program after object engine is created from REngine. did all the path settings correctly. can u please tell me what is the exact problem. it would be very kind of you to help

    Rengine engine = new Rengine(new String[] {“–save”}, true, null);
    System.out.println(“hello”);
    engine.eval(“.setenv <- if (exists("Sys.setenv")) Sys.setenv else Sys.putenv");

    engine.eval(".setenv("JAVAGD_CLASS_NAME"="JavaGDExample")");
    engine.eval("library(JavaGD)");
    engine.eval("JavaGD()");
    engine.eval("plot(rnorm(100))");

    Reply
  7. Johng123

    You must indulge in a contest for among the greatest blogs over the internet. Ill suggest this web website! gegckeeaaebe

    Reply
  8. WoonGooKim

    Hi
    I make source code as below statements as you suggested
    //
    package R;

    import java.awt.Component;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;

    import javax.swing.JFrame;

    import org.rosuda.JRI.Rengine;
    import org.rosuda.javaGD.GDInterface;
    import org.rosuda.javaGD.JGDBufferedPanel;

    public class JavaGDExample1 extends GDInterface implements WindowListener{
    JFrame f;

    public void gdOpen(double w, double h) {
    if (f!=null) gdClose();
    f = new JFrame(“JavaGD Example”);
    f.addWindowListener(this);
    c = new JGDBufferedPanel(w, h);
    f.getContentPane().add((Component) c);
    f.pack();
    f.setVisible(true);
    }

    public void gdClose() {
    super.gdClose();
    if (f!=null) {
    c=null;
    f.removeAll();
    f.dispose();
    f=null;
    }
    }

    public static void main(String[] args) {
    Rengine engine = new Rengine(new String[] {“–vanilla”}, true, null);

    engine.eval(“.setenv <- if (exists('Sys.setenv')) Sys.setenv else Sys.putenv");
    engine.eval(".setenv('JAVAGD_CLASS_NAME'='JavaGDExample1')");
    engine.eval("library(JavaGD)");
    engine.eval("JavaGD()");
    engine.eval("plot(rnorm(100))");

    }

    /** listener response to "Close" – effectively invokes

    dev.off()

    on the device */
    public void windowClosing(WindowEvent e) {
    if (c!=null) executeDevOff();
    }
    public void windowClosed(WindowEvent e) {}
    public void windowOpened(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
    }

    //

    I run this program in windows and eclipse not unix.
    I don't receive any console message. maybe program runs correctly. but Jframe is not opened.

    can you help me?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *