Load “rJava” package on R

Recently, some R users (include myself) find that they are unable to load ‘rJava’ package in R. This creates a big headache to the regular R user, since ‘rJava’ package is a core dependent package for most of txt processing R packages, like xlsx, tabulizer, Rwordseg, etc.).

It is crucial to fix this issue and load ‘rJava’ package on R successfully. Therefore, I’ve spent some time to find a way to make it work. Here are the steps.

0.1 Step 1: Make sure your R version in the RStudio is 32-bit.

When the R is installed on the computer, it automatically installed 2 versions.

R version

Figure 1: R version

Both versions are good to use for regular R programming. However, some of the txt process packages, like Rwordseg, are only available in the 32-bit. Please set up your R in RStudio to 32-bit.

Go to RStudio, click:

Tools > Global Options > General,R version:

Change to 32-bit

Figure 2: Change to 32-bit

0.2 Step 2: Install or reinstall Java on your PC

Before install ‘rJAVA’ package, you need make sure that the java installed on your PC is the most up-to-date.

Remember: Install Java JDK, not the regular JRE.

You can find the download link below:

JAVA jdk download

java download

Figure 3: java download

Download the setup file and install it on your computer (you may need seek help from IT department, since you may not have administrator authority for your computer). If it’s your personal computer, then never mind:)

Once the JDK installation process is complete, please check whether the installation is successful or not. In WIN10, follow the steps below.

  • click ‘Search Window’ next to windows button
  • type cmd to open command window
  • type java -version then you’ll see the the version of JAVA displayed
java version

Figure 4: java version

0.3 Step 3: Find your java folder on your PC and configure path on your PC

On your computer, find the folder where your java is installed:

java folder

Figure 5: java folder

Configure path on your PC: click ‘Search Window’ next to windows button, then type cmd.

Look for “rundll32.exe sysdm.cpl,EditEnvironmentVariables”

(see below).

rundll32.exe

Figure 6: rundll32.exe

Open a window, then add new User Variable-“JAVA_HOME” and put the Java path in the text dialog box (i.e., ‘C:Files (x86)1.8.0_161’, see below).

new user variable

Figure 7: new user variable

0.4 Step 4: Configure JAVA_HOME path on your RStudio

See the code block below.

R code

Figure 8: R code

You can also find the code below.

R code

Ou Zhang
Ou Zhang
Research Scientist & Data Scientist

I’m a research scientist, psychometrician, and data scientist, who loves psychometrics, applied statistics, general data science, and programming.

Related