Install rJava in Ubuntu with Oracle Java

Ubuntu does not come with any Java installed, unlike Linux Mint which comes at least with openJDK. The rJava package in R requires a JDK to exist in the system. If one uses Oracle Java, then at this point, he may want to install Oracle Java instead of openJDK. This process is very simple:
1. Download Oracle JDK archive
2. Extract to a location with user access, say $HOME/apps/jdk1.7
3. Add/ modify 2 environment variables: Create/ open $HOME/.bashrc and add to the end:

PATH=$PATH:$HOME/apps/jdk1.7/bin
export PATH

JDK_HOME=$HOME/apps/jdk1.7
export JDK_HOME

4. Since this is the only java, we dont need the update-alternatives. That would be required in Mint, since the default /usr/bin/java would be provided by the pre-installed openJDK.

Now, we want to configure Java for R, using

$ sudo R CMD javareconf

This searches for java in the PATH, then configures R to use that Java (does not modify any system environment variable). The problem with this command is that by default, it picks JAVA_HOME as $JDK_HOME/jre, instead of $JDK_HOME. Because of this, R thinks that only JRE exists in the system, and not JDK (which it requires). Hence, instead of above command, we give

$ sudo R CMD javareconf JAVA_HOME=/home/user/apps/jdk1.7

where ‘user’ is the username. This is the JAVA_HOME that we want R to have in its javaconf, because this is the JDK_HOME and not JRE_HOME.

Also, the JAVA_HOME system environment variable takes precedence over the one in R javaconf, hence we must remove it. If

$ echo $JAVA_HOME

returns something, it means at some point we had set the variable, so we must remove it. The command for R javareconf earlier would not set it, because as mentioned it does not create/ modify any system environment variables. To remove it,

$ unset JAVA_HOME

At this point, we have configured R javaconf, and ensured that JAVA_HOME is not set in the system. It therefore exists only in R javaconf. Open R (or RStudio), and install rJava:

> install.packages("rJava")

If behind a proxy, we can set R env var http_proxy. Open $HOME/.Renviron (create if does not exist) and add the line having format:

http_proxy=http://myproxyserver.com:port

Advertisements

One thought on “Install rJava in Ubuntu with Oracle Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s