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

2019-07-28 16:28发布

问题:

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?

回答1:

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()


标签: java jvm tooling