javajvmtooling

Correct way of referencing tools.jar in a cross-platform way


I'm building a JVM diagnostics tool and I'm connecting to the JVMs using JMX, the Issue I'm facing here is that I need to be able to load tools.jar in a way that is compatible throughout unixes and windows, currently I'm using the gradle-generated run scripts to load the tools.jar to the classpath but it seems this method depends on too many factors.

Does anyone knows the recommended or elegant way of making this jar available on the classpath without actually blindly guessing where is it?


Solution

  • ToolProvider.getSystemToolClassLoader()

    This returns the instance of ClassLoader that is aware of tools.jar.
    If you'd like the exact path, use

        ((URLClassLoader) ToolProvider.getSystemToolClassLoader()).getURLs()