1

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 Answers1

4

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()
apangin
  • 92,924
  • 10
  • 193
  • 247
  • Thank you for this, is there a command line switch to have these classes in the classpath already loaded? – Juan Alberto López Cavallotti Sep 02 '14 at 18:55
  • @JuanAlbertoLópezCavallotti I'm not sure what the problem is. Why not simply append `$JAVA_HOME/lib/tools.jar` to `-classpath` in a run script? Or if you need to load it in run-time, use `ToolProvider`. – apangin Sep 02 '14 at 20:08
  • Well this might be in different locations according to the installation used, I.E if JAVA_HOME is set to JDK_HOME/jre (which is a valid configuration) then the tools.jar would be in $JAVA_HOME/../lib/tools.jar so I'm trying to avoid having many entries on -classpath that point to different locations. Nevertheless, do you know the reason why a JDK would not load these in the app classloader by default? – Juan Alberto López Cavallotti Sep 03 '14 at 10:56
  • @JuanAlbertoLópezCavallotti That's how ToolProvider works. If it finds that `JAVA_HOME` ends with "/jre", it loads `../lib/tools.jar`, otherwise `lib/tools.jar`. You can implement the similar logic in the run script. Tools were not included in JRE in order to reduce JRE download size. [Project jigsaw](http://openjdk.java.net/projects/jigsaw/) may change the things by allowing modules to be loaded on demand. – apangin Sep 06 '14 at 10:31