Skip to content

Honor the JAVA_HOME environment variable #623

Closed

Description

@stoty

Is your feature request related to a problem? Please describe.
Visualvm does not honor the JAVA_HOME variable. Having to start it with "bin/visualvm --jdkhome $JAVA_HOME" is an unneccessary annoyance.

Describe the solution you'd like
If JAVA_HOME is set, then visualVM should use it as the default value for the jdkhome.
If it is not set, visualvm should behave as it does today.

Describe alternatives you've considered
None

Additional context
JAVA_HOME is used by most Java startup scritps to locate the JRE to be used.
Developers expect JAVA_HOME to be honored, and is usually set correctly on developer machines by jenv, sdkman, etc.

Activity

  1. dasarathirout commented on Jul 20, 2025

    @dasarathirout
    Member

    we can add configure visualvm_jdkhome in visualvm/etc/visualvm.conf file, then visualvm need no addinational arguments to run

    Image
  2. stoty commented on Jul 28, 2025

    @stoty
    Author

    Yes, but setting that is still a separate setup step, so it doesn' really solve the issue.

  3. stoty commented on Jul 28, 2025

    @stoty
    Author

    This has already been fixed for Windows in #112

  4. stoty commented on Jul 28, 2025

    @stoty
    Author

    So the logic / JVM precendence could be copied from there to make sure that it behaves consistently between Unix and Windows.

  5. dasarathirout commented on Jul 31, 2025

    @dasarathirout
    Member

    PR #643 Added JAVA_HOME to visulavm luncher & conf

  6. thurka commented on Aug 6, 2025

    @thurka
    Member

    @stoty Can you, please, describe in which situation you rely on JAVA_HOME? The current state is there for a long time, so it would be useful to know your use case. Note that NetBeans Platform code is primarily responsible for finding the JDK, so the problem should be ideally fixed there.

  7. stoty commented on Aug 6, 2025

    @stoty
    Author

    I am using the standalone visualvm distribution.

    The JVM is chosen by the startup bash script on Unix, which chooses the JVM for running visualvm.
    I'm not sure what you mean by the NetBeans platform code.
    Possibly visualvm is started differently when it is started as a NetBeans module (as opposed to the standalone visualvm startup script) , but then this patch does not apply in that case.

    I am working on multiple versions of multiple projects, which require different java versions, and use jenv to switch between them. All of my other tools (well, except for Eclipse) honor the JAVA_HOME environment variable.

    Also, this patch only aligns the unix startup script behaviour with the existing Windows behaviour, which already honor JAVA_HOME (see my comment above with the commit )

  8. thurka commented on Aug 6, 2025

    @thurka
    Member

    I'm not sure what you mean by the NetBeans platform code.

    If you do not specify JDK explicitly, it is chosen by NetBeans platform code, not by VisualVM launcher script.

    I am working on multiple versions of multiple projects, which require different java versions

    I see, but you are not required to run VisualVM on the same JDK as your project. VisualVM can simultaneously monitor Java processes running on different JDKs (JDK 6 up to JDK 24), so the JDK on which VisualVM is running has no connection to the JDK of your monitored processes.

    patch only aligns the unix startup script behaviour with the existing Windows

    Windows is a different story. The GH-112 fix in Windows is basically a workaround for VisualVM not starting at all in some situations (see discussion at GH-112). Again, on Windows, the JDK should be chosen by the NetBeans Platform code. Currently there are some historical changes in VisualVM Windows launcher regarding JDK selection, but they are unrelated to JDK_HOME. So unless this is a critical problem, I do not want to apply a similar workaround to the Unix launcher in VisualVM.

    It looks like NetBeans Platform updated the Windows code to consider JAVA_HOME (See NB-8408) so GH-112 could be removed once we update VisualVM to use NetBeans Platform 26.

  9. stoty commented on Aug 6, 2025

    @stoty
    Author

    If you do not specify JDK explicitly, it is chosen by NetBeans platform code, not by VisualVM launcher script.

    That does not match my experience.
    If you do not add the cli option or set it in the command line, then VisualVM will not find any JVM, and will not start.

  10. stoty commented on Aug 6, 2025

    @stoty
    Author

    also, there is heuristics in the VisualVM laucher script to find the JVM.

  11. dasarathirout commented on Aug 6, 2025

    @dasarathirout
    Member

    visualvm script call to nbexec script which exepect --jdkhome argument

    ./nbexec --help
    Usage: ./nbexec {options} arguments

    General options:
      --help                show this help 
      --jdkhome <path>      path to Java(TM) 2 SDK, Standard Edition
      -J<jvm_option>        pass <jvm_option> to JVM
    
      --cp:p <classpath>    prepend <classpath> to classpath
      --cp:a <classpath>    append <classpath> to classpath
    The operation couldn’t be completed. Unable to locate a Java Runtime.
    Please visit http://www.java.com for information on installing Java.
    
    Cannot find java. Please use the --jdkhome switch.
    

    From visualvm script we are composeing the arguments & delegate to nbexec

    /bin/bash /Users/Documents/oracle.visualvm/visualvm/dist/visualvm/platform/lib/nbexec 
    --userdir '/Users/Library/Application Support/VisualVM/dev' 
    --cachedir /Users/Library/Caches/VisualVM/dev 
    --jdkhome /Library/Java/JavaVirtualMachines/JDK17GVM 
    -J-Xdock:name=VisualVM 
    -J-Xdock:icon=/Users/Documents/oracle.visualvm/visualvm/dist/visualvm/etc/visualvm.icns 
    --branding visualvm 
    --clusters /Users/Documents/oracle.visualvm/visualvm/dist/visualvm/visualvm: 
    -J-Xms24m 
    -J-Xmx768m 
    -J-Dnetbeans.accept_license_class=org.graalvm.visualvm.modules.startup.AcceptLicense 
    -J-Dnetbeans.importclass=org.graalvm.visualvm.modules.startup.ImportSettings 
    -J-Dsun.jvmstat.perdata.syncWaitMs=10000 
    -J-Dsun.java2d.noddraw=true 
    -J-Dsun.java2d.d3d=false 
    -J-DTopSecurityManager.disable=true 
    -J-Dorg.netbeans.core.TimeableEventQueue.quantum=360000 
    -J-Dpolyglot.js.nashorn-compat=true 
    -J-Dsun.misc.URLClassPath.disableJarChecking=true 
    -J-Djdk.attach.allowAttachSelf=true 
    -J-Dorg.openide.util.ImageUtilities.level=950 
    -J--add-exports=java.desktop/com.sun.java.swing.plaf.gtk=ALL-UNNAMED 
    -J--add-exports=java.desktop/sun.awt=ALL-UNNAMED 
    -J--add-exports=jdk.internal.jvmstat/sun.jvmstat.monitor.event=ALL-UNNAMED 
    -J--add-exports=jdk.internal.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED 
    -J--add-exports=java.desktop/sun.swing=ALL-UNNAMED 
    -J--add-exports=jdk.attach/sun.tools.attach=ALL-UNNAMED 
    -J--add-opens=java.desktop/sun.awt.X11=ALL-UNNAMED 
    -J--add-opens=java.desktop/javax.swing.plaf.synth=ALL-UNNAMED 
    -J--add-opens=java.base/java.net=ALL-UNNAMED 
    -J--add-opens=java.base/java.lang.ref=ALL-UNNAMED 
    -J--add-opens=java.base/java.lang=ALL-UNNAMED 
    -J--add-opens=java.desktop/javax.swing=ALL-UNNAMED 
    -J--add-opens=java.desktop/javax.swing.plaf.basic=ALL-UNNAMED 
    -J-XX:+IgnoreUnrecognizedVMOptions 
    -J-Dnetbeans.logger.console=true 
    -J-Dnetbeans.exception.report.min.level=900 
    -J-ea
    
    
  12. thurka commented on Aug 8, 2025

    @thurka
    Member

    If you do not specify JDK explicitly, it is chosen by NetBeans platform code, not by VisualVM launcher script.

    That does not match my experience. If you do not add the cli option or set it in the command line, then VisualVM will not find any JVM, and will not start.

    It works fine. In general, you are not required to specify the JDK. Of course there could be system configuration, when JDK is not found. That is why I asked for your use-case.

  13. thurka commented on Aug 8, 2025

    @thurka
    Member

    visualvm script call to nbexec script which exepect --jdkhome argument

    VisualVM launcher does not require JDK. If you do not specify it, the nbexec invocation looks like this:

    + exec /bin/bash /home/thurka/Projects/visualvm_219/platform/lib/nbexec
    --userdir /home/thurka/.visualvm/2.1.9
    --cachedir /home/thurka/.cache/visualvm/2.1.9
    --jdkhome 
    --branding visualvm
    --clusters /home/vscode/Projects/visualvm_219/visualvm:
    -J-Xms24m
    -J-Xmx768m
    -J-Dnetbeans.accept_license_class=org.graalvm.visualvm.modules.startup.AcceptLicense
    -J-Dnetbeans.importclass=org.graalvm.visualvm.modules.startup.ImportSettings
    -J-Dsun.jvmstat.perdata.syncWaitMs=10000
    -J-Dsun.java2d.noddraw=true
    -J-Dsun.java2d.d3d=false
    -J-Dorg.netbeans.core.TimeableEventQueue.quantum=360000
    -J-Dpolyglot.js.nashorn-compat=true
    -J-Dsun.misc.URLClassPath.disableJarChecking=true
    -J-Djdk.attach.allowAttachSelf=true
    -J-Dorg.openide.util.ImageUtilities.level=950
    -J--add-exports=java.desktop/com.sun.java.swing.plaf.gtk=ALL-UNNAMED
    -J--add-exports=java.desktop/sun.awt=ALL-UNNAMED
    -J--add-exports=jdk.internal.jvmstat/sun.jvmstat.monitor.event=ALL-UNNAMED
    -J--add-exports=jdk.internal.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED
    -J--add-exports=java.desktop/sun.swing=ALL-UNNAMED
    -J--add-exports=jdk.attach/sun.tools.attach=ALL-UNNAMED
    -J--add-opens=java.desktop/sun.awt.X11=ALL-UNNAMED
    -J--add-opens=java.desktop/javax.swing.plaf.synth=ALL-UNNAMED
    -J--add-opens=java.base/java.net=ALL-UNNAMED
    -J--add-opens=java.base/java.lang.ref=ALL-UNNAMED
    -J--add-opens=java.base/java.lang=ALL-UNNAMED
    -J--add-opens=java.desktop/javax.swing=ALL-UNNAMED
    -J--add-opens=java.desktop/javax.swing.plaf.basic=ALL-UNNAMED
    -J-XX:+IgnoreUnrecognizedVMOptions
    
    
  14. stoty commented on Aug 8, 2025

    @stoty
    Author
    stoty@stoty-Precision-5570:~/visualvm_22/bin  $ ./visualvm
    Cannot find java. Please use the --jdkhome switch.
    stoty@stoty-Precision-5570:~/visualvm_22/bin  $ 
    

    There is no visualvm.exe for unix.
    Either the bash script finds a java and uses it to start visualvm/netbeans or it fails.

  15. dasarathirout commented on Aug 9, 2025

    @dasarathirout
    Member

    nbexec#L130-L179 script checking jdk location , but it is not considering JAVA_HOME env var to be used as jdkhome.

  16. stoty commented on Aug 11, 2025

    @stoty
    Author

    Thanks @dasarathirout .

    You are right, it makes sense to fix this problem there, and remove the redundant JAVA_HOME code from 'visualvm"
    Seems like nbexec simply is just unaware of the existence of non Apple Unix OSs.

    (and even the MacOS logic could use some improvments)

  17. stoty commented on Aug 11, 2025

    @stoty
    Author

    Actually, it is aware (there is a default case), but it ignores JAVA_HOME, and the symlink resolution code doesn't work with jenv style shims.

  18. thurka commented on Aug 11, 2025

    @thurka
    Member

    There is no visualvm.exe for unix.

    Right, but I was never talking about visualvm.exe.

    Either the bash script finds a java and uses it to start visualvm/netbeans or it fails.

    Right, but there are two scripts. bin/visualvm startup script does not search for jdk. The second script platform/lib/nbexec is responsible for launching java binary and can find the JDK if you do not specify it. The fact that it fails for you does not mean that it fails for everyone.

  19. stoty commented on Aug 11, 2025

    @stoty
    Author
  20. thurka commented on Aug 11, 2025

    @thurka
    Member

    nbexec#L130-L179 script checking jdk location , but it is not considering JAVA_HOME env var to be used as jdkhome.

    .. and this is the right place for the fix. I already mentioned it here: #623 (comment)

  21. neilcsmith-net commented on Aug 18, 2025

    @neilcsmith-net

    The right place for any platform application to have alternative launcher behaviour to the default is first and foremost its own launch script!

  22. self-assigned this
    on Feb 1, 2026
  23. added a commit that references this issue on Feb 4, 2026
  24. thurka commented on Feb 7, 2026

    @thurka
    Member

    Done in master.

  25. woj-tek commented on Apr 10, 2026

    @woj-tek

    (I'm not sure if it's better to open new issue or comment in this one but for consistency I'll comment in here).

    Giving more context to @thurka :

    "Can you, please, describe in which situation you rely on JAVA_HOME?":

    I'm using mise (https://mise.jdx.dev/) to manage java installation. It handles setting up environment (including JAVA_HOME) and in general works well everywhere. However, for some reason it doesn't work with VisualVM.

    🕙 [09:11:10] ❯ echo $JAVA_HOME
    /Users/work/.local/share/mise/installs/java/25.0.2
    
    🕙 [09:17:08] ❯ java --version
    openjdk 25.0.2 2026-01-20
    OpenJDK Runtime Environment (build 25.0.2+10-69)
    OpenJDK 64-Bit Server VM (build 25.0.2+10-69, mixed mode, sharing)
    
    🕙 [09:17:02] ❯ /Applications/VisualVM.app/Contents/MacOS/visualvm
    The operation couldn’t be completed. Unable to locate a Java Runtime.
    Please visit http://www.java.com for information on installing Java.
    
    Cannot find java. Please use the --jdkhome switch.
    

    It works fine if I specify jdkhome:

    🕙 [09:18:48] ❯ /Applications/VisualVM.app/Contents/MacOS/visualvm --jdkhome /Users/work/.local/share/mise/installs/java/25/
    WARNING: package com.sun.java.swing.plaf.gtk not in java.desktop
    WARNING: package sun.awt.X11 not in java.desktop
    WARNING: A restricted method in java.lang.System has been called
    WARNING: java.lang.System::loadLibrary has been called by com.sun.jna.Native in an unnamed module (jar:file:/Applications/VisualVM.app/Contents/Resources/visualvm/platform/modules/ext/jna-5.14.0.jar!/)
    WARNING: Use --enable-native-access=ALL-UNNAMED to avoid a warning for callers in this module
    WARNING: Restricted methods will be blocked in a future release unless native access is enabled
    
    -------------------------------------------------------------------------------
    >Log Session: Friday, April 10, 2026, 9:19:26 AM Central European Summer Time
    >System Info:
      Product Version         = VisualVM 2.2.1
      Operating System        = Mac OS X version 15.7.4 running on aarch64
      Java; VM; Vendor        = 25.0.2; OpenJDK 64-Bit Server VM 25.0.2+10-69; Oracle Corporation
      Runtime                 = OpenJDK Runtime Environment 25.0.2+10-69
      Java Home               = /Users/work/.local/share/mise/installs/java/25.0.2
      System Locale; Encoding = en_ES (visualvm); UTF-8
      Home Directory          = /Users/work
      Current Directory       = /Users/work
      User Directory          = /Users/work/Library/Application Support/VisualVM/2.2.1
      Cache Directory         = /Users/work/Library/Caches/VisualVM/2.2.1
      Installation            = /Applications/VisualVM.app/Contents/Resources/visualvm/visualvm
                                /Applications/VisualVM.app/Contents/Resources/visualvm/platform
    …
    

    Version 2.2.1 lists this issue in its release notes, so I'm not sure why it's not picking the JDK from th environment variable.

  26. thurka commented on Apr 10, 2026

    @thurka
    Member

    @woj-tek It looks like the backported fix does not cover macOS. Please file separate issue. Thanks.

  27. woj-tek commented on Apr 10, 2026

    @woj-tek

    Done. Thank you for confirming :)

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions