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) {}
}
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à:

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.
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[latex cssclass="latex-inline"]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[/latex]AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
… 4 more
Do you have any suggestions?
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:.
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
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:
engine.eval("library(JavaGD)");
engine.eval("JavaGD()");
engine.eval("plot(rnorm(100))");
But that’s not the point.
Best regards, Kornelius.
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