5

For security reasons I am forced to de-install Java (JRE) on a machine I am using with R.

How can I (easily :-) discover all installed packages that use Java?

Edit 14.12.2021: The log4j-log4shell-cve-2021-44228-vulnerability makes this question (and answers) even more interesting ;-)

R Yoda
  • 8,358
  • 2
  • 50
  • 87
  • so you need to un-install a `jre` and all other software that might be using it? – Eugene Nov 30 '17 at 21:08
  • Sorry for using the "java" tag, this is probably more an R question. Yes, but R is implemented mainly in C and most packages (libraries) in R, C and C++. I just want to de-install those R packages that call Java/JRE – R Yoda Nov 30 '17 at 21:11
  • ah! sorry can't help u here – Eugene Nov 30 '17 at 21:12

2 Answers2

5

You can use installed.packages to determine which packages import the rJava package. You need to tell installed.packages to include the Imports field from the package description, and then check which packages import rJava.

LIBS = installed.packages(fields=c("Imports"))
JPacks = grep("Java", LIBS[,"Imports"], ignore.case=TRUE)
LIBS[JPacks, c("Package", "Imports")]
          Package    
RWeka     "RWeka"    
RWekajars "RWekajars"
          Imports                                                                
RWeka     "RWekajars (>= 3.9.0), rJava (>= 0.6-3), graphics, stats,\nutils, grid"
RWekajars "rJava (>= 0.6-3)"
G5W
  • 36,531
  • 10
  • 47
  • 80
3

I have extended the solution of @GSW's answer by also considering other types of dependencies from the rJAva package:

libs       = installed.packages()

imports    = grep("Java", libs[,"Imports"],   ignore.case=TRUE)
depends    = grep("Java", libs[,"Depends"],   ignore.case=TRUE)
linking.to = grep("Java", libs[,"LinkingTo"], ignore.case=TRUE)
enhances   = grep("Java", libs[,"Enhances"],  ignore.case=TRUE)
# SystemRequirements may also contain Java dependencies but is not available in the matrix

libs[c(imports, depends, linking.to, enhances),
     c("Package", "Imports", "Depends", "LinkingTo", "Enhances")]

This now also finds eg. xlsx:

         Package    Imports Depends           LinkingTo Enhances
xlsx     "xlsx"     NA      "rJava, xlsxjars" NA        NA      
xlsxjars "xlsxjars" NA      "rJava"           NA        NA      

Edit Dec 21, 2021: If you want to find all CRAN packages (not only the installed ones) that directly depend on JAVA (eg. due to log4j vulnerability) you can use:

# Dependencies external to the R system should be listed in the `SystemRequirements` field of the package's DESCRIPTION file.
# This also holds true until the package uses Java via the rJava package where the `Imports` or `Depends` declaration suffices:
# https://cran.r-project.org/doc/manuals/R-exts.html#Non_002dR-scripts-in-packages

CRAN.pkgs <- tools::CRAN_package_db()  # gets a list of all R packages at CRAN

imports    = grepl("Java", CRAN.pkgs$Imports,             ignore.case = TRUE)
depends    = grepl("Java", CRAN.pkgs$Depends,             ignore.case = TRUE)
linking.to = grepl("Java", CRAN.pkgs$LinkingTo,           ignore.case = TRUE)
enhances   = grepl("Java", CRAN.pkgs$Enhances,            ignore.case = TRUE)
sysreq     = grepl("Java", CRAN.pkgs$SystemRequirements,  ignore.case = TRUE)

CRAN.java.pkgs <- CRAN.pkgs[imports | depends | linking.to | enhances | sysreq,
                            c("Package", "Imports", "Depends", "LinkingTo", "Enhances", "SystemRequirements")]

NROW(CRAN.pkgs)         # more than 18.000 in 12/2021
NROW(CRAN.java.pkgs)    #              137 in 12/2021

CRAN.java.pkgs$Package  # show all packages found
R Yoda
  • 8,358
  • 2
  • 50
  • 87