Calling R from Java using JDeveloper

Download: rJava0.7 from http://www.rforge.net/rJava/files/

Extract from “rJava 0.7\rJava\jri\JRI.jar” and put the .class files inside your project. Still not able to compile, also add the JRI.jar file to the library path (for compilation purposes):

For the sake of convenience, put “import org.rosuda.JRI.*;” everywhere although every class specifically mentioned is better of course.

Now, a more common error is shown on the console when trying to use the R engine:

Cannot find JRI native library!
Please make sure that the JRI native library is in a directory listed in java.library.path.
java.lang.UnsatisfiedLinkError: no jri in java.library.path

Obviously the JRI.jar is not on the library path (but the swing application contains it -they should work on that).

Still, on any client ‘R’ will be installed; which contains the JRI.jar if rJava gets installed. So, let’s do that first, It is adviced to use version 2.9, so let’s do a full-install with 2.9.1 (if that will fail, we try 2.9.0).

In Vista, install it in the user environment as administrator:

Then set R_HOME to the proper location and add R_HOME/bin to the Windows path:

Although this should work immediately, the paranoia mode is fully operational, so reboot first.

After a reboot, run ‘R’ and install rJava using this command:

install.packages('rJava',,'http://www.rforge.net/')

Now the JRI.jar should be in the library/rJava/jri directory and it should be the same (hopefully) as in the JDeveloper project(!). This part is rather buggy in my optinion, since we only can assume the intall.packages command uses exactly the same as the version in the files section mentioned in the very first line of this post.

The ‘UnsatisfiedLinkError’ is still there when running a test from JDeveloper, so after some googling the native DLLs must be on the system path as well, in this case the jri.dll (pretty clear exception, not). This is an important step for the installation manual.

When the R_HOME/library/rJava/jri is added to the system path, the test from inside JDeveloper works! Now let’s go for a deployment on another machine. It is clear, the development machine is a bit more intelligent than one without a Java SDK (only JRE 1.6 is required). When all system variables are set correctly (nu user environment variable), all works nicely.

For the future; not to include the JRI.jar inside the deployment.

Other font in JTable cell

While doing some research, I was looking for an efficient way to change the font inside a JTable. I have made my own TableCellRenderer’s before, but while googling I came across the following link:

http://www.exampledepot.com/egs/javax.swing.table/CustRend.html

Beside the fact I wanted to make a certain cell italic, the last few lines somehow intrigued me to find out more about the author of this Java Developers Almanac, which is Patrick Chan. Great book, but not really my kinda book. Anyway, here is the TableCellRenderer which can be used to create a column in a JTable which has an italic font (thus another font, which I was looking for) :

import java.awt.Component;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;

public class TableCellRenderer_Italic extends JLabel implements TableCellRenderer {
    // used to pad the cell when it does not have focus
    protected static Border normalBorder = new EmptyBorder(1, 1, 1, 1);

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        if(isSelected) {
            setForeground(table.getSelectionForeground());
            setBackground(table.getSelectionBackground());
        } else {
            setForeground(table.getForeground());
            setBackground(table.getBackground());
        }
        if(hasFocus) {
            setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
        } else {
            setBorder(normalBorder);
        }
        setFont(new Font(this.getFont().getName(), Font.ITALIC, this.getFont().getSize()));
        setText(value.toString());
        return this;
    }
    // the following methods overide the defaults for performance reasons, lines thanks to Patrick Chan
    public void validate() {}
    public void revalidate() {}
    protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {}
    public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}
}

Note: to use it in another class:

private TableCellRenderer itaRenderer = new TableCellRenderer_Italic();
myTable.getColumnModel().getColumn(3).setCellRenderer(itaRenderer);

Getting date time from MySQL using Java JDBC

Since I seem to forget how to get/convert dates from the database into Java using JDBC (I use Oracle and MySQL for different projects), a reminder:

Suppose there is a MySQL table structure like:

create table prp_models (
  prp_id        int(10) unsigned not null auto_increment,
  prp_created   datetime default null,
  prp_
)

Then a proper way to get the prp_created column from a resultset (res) is:

java.sql.Timestamp myDateTime = res.getTimestamp(2);  // res #1 is the ID

If you want to get the date time in a displayable format (String), you can use:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(myDateTime.getTime());

Note on the side: within Java you can set myDateTime with system time using:
myDateTime = new Timestamp(System.currentTimeMillis());