javarmaxent

JVMJ9VM015W Initialization error for library j9gc29(2): Failed to instantiate compressed references metadata. 200M requested


I'm following a tutorial on GitHub by John Baums to create a presence only species distribution model using the MAXENT method.

Every line of code is working until I need to run the object me, which is the function that calls Maxent's Java program, maxent.jar, and I get that error message below.

I tried to install rJava to run this line of code again:

#Install the package rJava
install.packages('rJava')

#Open the library rJava
library (rJava)

Once I've installed rJava and tried to run the code again, my session in RStudio is aborted:

R session Aborted where R encounters a fatal error

You can download all the data needed with the code below to reproduce this problem.

Does anyone know how to fix this issue?

library(viridis)
library (devtools)
library(dismo)
library(raster)

#Install the pacakge rasterVis
devtools::install_github('oscarperpinan/rasterVis')

#Open the library rasterVis
library(rasterVis)

#Install the package rmaxent
install.packages("remotes")
remotes::install_github("johnbaums/rmaxent")

#Open the library rmaxent
library(rmaxent)

#Let's import the B. variegatus occurrence and predictor data from the appropriate paths:

occ_file <- system.file('ex/bradypus.csv', package='dismo')
occ <- read.table(occ_file, header=TRUE, sep=',')[,-1]

library(raster)
pred_files <- list.files(system.file('ex', package='dismo'), '\\.grd$', full.names=TRUE )
predictors <- stack(pred_files)

#The object predictors is a RasterStack comprising nine raster layers, one for each #predictor used in the model. We can now fit the model using the maxent function from the #dismo package. Note that this function calls Maxent's Java program, maxent.jar

me <- maxent(predictors, occ, factors='biome', args=c('hinge=false', 'threshold=false'))

Error message:

Loading required namespace: rJava
JVMJ9VM015W Initialization error for library j9gc29(2): Failed to instantiate compressed references metadata.  200M requested
Error in .jinit(parameters = parameters) : 
  Cannot create Java virtual machine (-4)

Solution

  • You need to have Java installed for this code to work. You can download Java from here. The following was tested on a Windows PC using Java for Windows downloaded here. Just use the default install location.

    The macOS version that worked for the OP, JAVA 23, can be found here.

    After installing Java, you can check that your system is configured correctly by running system("java -version") in an R console. It should look similar to this:

    system("java -version")
    # java version "1.8.0_421"
    # Java(TM) SE Runtime Environment (build 1.8.0_421-b09)
    # Java HotSpot(TM) 64-Bit Server VM (build 25.421-b09, mixed mode)
    # [1] 0
    

    If R can't see Java or the code still does not work, search online for how to configure R correctly. This will differ depending on your operating system.

    Also, the plot code did not work for me, so alternative plot code is provided.

    devtools::install_github("johnbaums/rmaxent")
    library(rmaxent)
    
    # Import data, create RasterStack
    occ_file <- system.file('ex/bradypus.csv', package='dismo')
    occ <- read.table(occ_file, header=TRUE, sep=',')[,-1]
    
    library(raster)
    pred_files <- list.files(system.file('ex', package='dismo'), '\\.grd$', full.names=TRUE )
    predictors <- stack(pred_files)
    
    # Fit model
    library(dismo)
    me <- maxent(predictors, occ, factors='biome', args=c('hinge=false', 'threshold=false'))
    
    prediction <- project(me, predictors)
    
    # Plot
    library(rasterVis)
    library(viridis)
    
    levelplot(prediction$prediction_logistic, margin=FALSE, col.regions=viridis, at=seq(0, 1, len=100),
              panel = function(...){
                panel.levelplot(...)
                sp.points(SpatialPoints(occ), pch = 20, col = 1)
              })
    
    # Compare timing (your results may vary)
    library(microbenchmark)
    timings <- microbenchmark(
      rmaxent=pred_rmaxent <- project(me, predictors),
      dismo=pred_dismo <- predict(me, predictors), 
      times=10)
    
    print(timings, signif=2) 
    # Unit: milliseconds
    #     expr min  lq mean median  uq max neval
    #  rmaxent  66  69   76     71  83  88    10
    #    dismo 230 240  290    250 290 530    10
    
    all.equal(values(pred_rmaxent$prediction_logistic), values(pred_dismo))
    # [1] "Mean relative difference: 0.2073864"
    
    # Examine model
    parse_lambdas(me)
    # Features with non-zero weights
    # 
    #        feature    lambda    min     max        type
    #   (biome==1.0)   1.49227      0       1 categorical
    #   (biome==2.0)   1.15527      0       1 categorical
    #   (biome==9.0)   2.33123      0       1 categorical
    #  (biome==13.0)   1.96180      0       1 categorical
    #  (biome==14.0)   0.32250      0       1 categorical
    #           bio1   6.60856    -23     289      linear
    #          bio16   0.32171      0    2458      linear
    #          bio17  -4.08709      0    1496      linear
    #           bio7 -16.18362     62     461      linear
    #           bio8   1.85855    -66     323      linear
    #         bio5^2  -2.16296   3721  178084   quadratic
    #         bio6^2  -5.26415      0   57600   quadratic
    #         bio7^2  -7.49172   3844  212521   quadratic
    #         bio8^2   0.12389      0  104329   quadratic
    #     bio12*bio7   4.15357      0  737464     product
    #     bio12*bio8   2.44714 -27324 2020366     product
    #     bio16*bio8   0.01359 -13332  638038     product
    #     bio17*bio7   0.07878      0  145705     product
    #      bio5*bio7  -3.27012   8235  162812     product
    # 
    # 
    # Features with zero weights
    # 
    #  feature lambda  min  max   type
    #    bio12      0    0 7682 linear
    #     bio5      0   61  422 linear
    #     bio6      0 -212  240 linear
    
    # Identify which variable is most responsible for decreasing suitability
    lim <- limiting(predictors, me)
    
    # Plot
    levelplot(lim, col.regions=rainbow,
              panel = function(...){
                panel.levelplot(...)
                sp.points(SpatialPoints(occ), pch = 20, col = 1)
              })
    
    
    # Improved model
    me2 <- maxent(predictors, occ, factors='biome', args=c('hinge=false', 'threshold=false', 'betamultiplier=5'))
    pred2 <- project(me2, predictors)
    
    # Compare
    ic(stack(pred_rmaxent$prediction_raw, pred2$prediction_raw), 
       occ, list(me, me2))
    #          n  k        ll      AIC     AICc      BIC
    # layer.1 94 19 -736.2354 1510.471 1520.741 1558.793
    # layer.2 94  9 -753.9800 1525.960 1528.103 1548.850