About rJava
GIT access
Download/Files
News
Check results
Package R docs
rJava is a simple R-to-Java interface. It is comparable to the .C/.Call C interface. rJava provides a low-level bridge between R and Java (via JNI). It allows to create objects, call methods and access fields of Java objects from R.
rJava release versions can be obtained from CRAN - usually install.packages("rJava") in R will do the trick. The current development version can be downloaded from the files section.
In a sense the inverse of rJava is JRI (Java/R Interface) which provides the opposite direction - calling R from Java. JRI is now shipped as a part of the rJava package, although it still can be used as a separate entity (especially for development). Currently rJava is used as a part of JGR, iPlots and JavaGD software/packages.
Please report any bugs or wishes related to rJava or JRI using Issues on GitHub.
What's new? rJava source repository is now on GitHub and that is also the place to report bugs. The main page and builds are still on RForge.net.2021-09-24 - rJava 1.0-5 released. This update is only important to JDK-17 users and make sure you read NEWS file as some existing packages may no longer work with JDK-17 due to some major changes.
2021/04/29 - rJava 1.0-4 released. This release fixes a possible segafult in .jfields() when used on class objects (also indirectly in J() API).
2021/04/15 - rJava 1.0-2 released. This release improves string handling between R and Java for characters outside of the Unicode BMP.
2021/04/12 - rJava 1.0-1 released. This is a major new release, the main feature is the introduction of custom class loaders for R package authors. R packages using Java are encouraged to use this new facility to avoid class clashes with other Java packages.
2021/02/04 - rJava 0.9-14 released. Mostly fixes in JRI compilations.
2020/07/06 - rJava 0.9-13 released. Minor fixes and increase default stack size.
2020/03/23 - rJava 0.9-12 released. Strongly recommended for macOS users, required for Catalina. Invalid JAVA_HOME settings are now reported. Also includes some minor bug-fixes.
...
2011/06/22 - rJava 0.9-0 released. This is a major upgrade that changes behavior of array references in low-level calls back to early 0.8 state as intedended by the original design. It should be more consistent now. We have had rJava 0.9-0 in RC state for a long time so hopefully all developers relying on rJava have checked their packages. For the full list of fixes and changes see NEWS.
2009/10/27 - rJava 0.8-0 released. Many new features mostly thanks to Romain Francois -- check the NEWS file for details.
2009/08/22 - rJava 0.7-0 released. Recommended update (includes bugfixes to fields handling, new support for with(), method/field auto-completion and more forgiving $ operator), includes JRI 0.5-0.
2008/09/22 - rJava 0.6-0 released. Adds support for Java serialization and R-side cache which enables Java objects to become persistent across sessions. See ?.jcache in R (or the online help) for details.
2007/08/22 - rJava 0.5-0 released. This is a major update featuring many new features and bugfixes. It sports a new custom class loader, much improved (and faster) field support, integration of all native Java types, automatic fall-back to static methods, infrastructure for writing Java packages easily (see .jpackage), support for custom convertors and call-backs. Please read the NEWS file for details.
2007/02/27 - rJava 0.4-14 (update is recommended to all users due to memory leak fixes), please use CRAN to get the latest release. The current development version is rJava 0.5-0 (available from here - see SVN access and download on the left). It is under heavy construction right now with many new features, so feel free to test-drive it if you want to be on the bleeding edge (it is a bit chatty as some debugging output is still enabled). Some of the highlights are memory profiler, intelligent class loader, easy Java package integration and callback support.
InstallationFirst, make sure you have JDK 1.4 or higher installed (some platforms require hgher version see R Wiki). On unix systems make sure that R was configured with Java support. If not, you can re-configure R by using R CMD javareconf (you may have to prepend sudo or run it as root depending on your installation - see R-ext manual A.2.2 for details). On Windows Java is detected at run-time from the registry.
rJava can be installed as any other R package from CRAN using install.packages('rJava'). See the files section in the left menu for development versions.
JRI is only compiled if supported, i.e. if R was configured as a framework or with --enable-R-shlib.
DocumentationIf you want to run R within a Java application, please see the JRI pages for details. rJava allows you to use R code to create Java objects, call Java methods and pass data between R and Java. Most functions are already documented by the corresponding help pages in R, but here is a very basic crashcourse:
The following gives a quick guide to the use of rJava. If you have questions about installation, please visit the R Wiki - rJava package.
Let's start with some low-level examples. Remember, this is all essentially a JNI interface, so you may encounter new things if you used Java from high level only.
library(rJava)
.jinit() # this starts the JVM
s <- .jnew("java/lang/String", "Hello World!")
Next, we will call a simple method:
.jcall(s,"I","length")
[1] 12
.jcall(s,"I","indexOf","World")
[1] 6
.jmethods(s,"concat")
[1] "public java.lang.String java.lang.String.concat(java.lang.String)"
.jcall(s,"Ljava/lang/String;","concat",s)
[1] "Hello World!Hello World!"
There is a simple function .jstrVal that returns the content of a string reference or calls toString() method and returns the string:
print(s)
[1] "Java-Object: Hello World"
.jstrVal(s)
[1] "Hello World"
f <- .jnew("java/awt/Frame", "Hello")
b <- .jnew("java/awt/Button", "OK")
.jcall(f, "Ljava/awt/Component;", "add", .jcast(b, "java/awt/Component"))
.jcall(f,, "pack")
.jcall(f,, "setVisible", TRUE)
Finally a note about the $ convenience operator. It provides an experimental, but simple way of writing code in Java style at the cost of speed. Taking the String example, you can achieve the same with:
s$length()
[1] 12
s$indexOf("World")
[1] 6
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4