Skip to content

Cannot reload rJava after detaching #249

Description

@Mosk915

When using version 0.9-13 of rJava, trying to reload the package after detaching it results in an error. This does not occur in version 0.9-12.

> library(rJava)
> detach("package:rJava", unload = TRUE)
> library(rJava)
Error: package or namespace load failed for ‘rJava’:
 .onLoad failed in loadNamespace() for 'rJava', details:
  call: FUN(X[[i]], ...)
  error: no such symbol PushToREXP in package rJava

Activity

  1. s-u commented on Dec 14, 2020

    @s-u
    Owner

    Version 0.9-13 uses (optionally) dynamic loading of JVM library so it cannot be unloaded since the JVM is loaded directly into R instead of rJava linking to JVM (this is the default on macOS to allow fo dynamic location of the JDK). I'm not sure if this is the cause, but rJava was never designed to support unloading, because JVM cannot be initialised twice anyway. The only thing we could do is to not load JVM until .jinit() which is the point of no return. What is the purpose of trying to detach rJava if it can not be detached once used anyway?

  2. s-u commented on Dec 14, 2020

    @s-u
    Owner

    BTW: see also #25 for the full discussion why detaching is not supported.

  3. Mosk915 commented on Dec 14, 2020

    @Mosk915
    Author

    The motivation was driven by the fact that it's not possible to use the XLConnect package and the xlsx package in the same R session, I believe since they both depend on rJava. By "same R session", I don't mean concurrently, since I know they will conflict, but just loading one package, then detaching it, and loading the other package. That still doesn't work, but I thought by detaching and reloading rJava it would solve the problem. It sounds like it wouldn't necessarily work anyway, in which case I'll just have to work around it by starting a new session if I need to use the second Excel package.

  4. s-u commented on Dec 14, 2020

    @s-u
    Owner

    You can use arbitrarily many packages that depend on rJava, there is no limit at all, so I don't see a reason why they shouldn't work. However, once you load any Java code you cannot unload the JVM so what you were trying to do won't work either way. You have to re-start the R process in order to start a new JVM.

  5. Mosk915 commented on Dec 14, 2020

    @Mosk915
    Author

    Thanks for clarifying. These are the two issues from a few years ago that say the two Excel packages can't be used in the same session because they share a Java dependency: miraisolutions/xlconnect#79 and colearendt/xlsx#95

    If I understand correctly, you're saying there's no reason why both packages couldn't be loaded and used concurrently? In any case, since this isn't an issue with rJava, I'll close the issue.

  6. s-u commented on Dec 14, 2020

    @s-u
    Owner

    @Mosk915 Ok, got it, so the issue is likely that they use the exactly same class, but each of them has a different version of it (from a different JAR file). In principle that is actually a solvable problem, because rJava has control over the class loader, so it could use separate class loaders for each package. Currently, rJava uses a global class loader, but it is in principle possible to do something like the namespaces in R where a package would register its own class loader and use it when calling its classes. This will require co-operation of the packages, but probably worthwhile to avoid cases like the above.

  7. Mosk915 commented on Dec 14, 2020

    @Mosk915
    Author

    That's good to hear it's solvable. Thanks so much for looking into it and for the quick responses.

  8. s-u commented on Sep 15, 2024

    @s-u
    Owner

    Just for the record since this was just referenced: since rJava 1.0 packages can use package-private class loaders (see own.loader=TRUE argument in .jpackage()) which guarantees that each package is isolated from any other packages and thus the problem referenced here (two packages loading conflicting classes in one session) is solved by that feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions