Search code examples
javajavahelp

JavaHelp: is it possible to load help from a location outside the classpath?


I would like the ability to load javahelp content from a custom location outside the classpath. This location could change during application's life and could be on a shared network device.

Unfurtunately, the HelpSet class is expecting a classloader, so I guess my helpset files must be in the classpath, or there is another way? Thanks in advance.


Solution

  • This should be possible via creating and using your own ClassLoader. The most likely candidate ClassLoader that you will want to use is URLClassLoader.

    You probably have code that looks something like this:

    JHelp helpViewer = null;
    try {
      // Get the classloader of this class.
      ClassLoader cl = JavaHelpTest.class.getClassLoader();
      // Use the findHelpSet method of HelpSet to create a URL referencing the helpset file.
      // Note that in this example the location of the helpset is implied as being in the same
      // directory as the program by specifying "jhelpset.hs" without any directory prefix,
      // this should be adjusted to suit the implementation.
      URL url = HelpSet.findHelpSet(cl, "jhelpset.hs");
      // Create a new JHelp object with a new HelpSet.
      helpViewer = new JHelp(new HelpSet(cl, url));
    }
    

    You need to change the line where you get your ClassLoader to get your own based on the shared directory, instead of the one based on the system class path. So something like this:

    JHelp helpViewer = null;
    try {
      // Get the class loader of the shared directory. Note that directories are
      // required to have a trailing '/' or '\'.
      ClassLoader cl = new URLClassLoader(new URL[] {new URL("file:///path/to/share/")});
      // Use the findHelpSet method of HelpSet to create a URL referencing the helpset file.
      // Note that in this example the location of the helpset is implied as being in the same
      // directory as the program by specifying "jhelpset.hs" without any directory prefix,
      // this should be adjusted to suit the implementation.
      URL url = HelpSet.findHelpSet(cl, "jhelpset.hs");
      // Create a new JHelp object with a new HelpSet.
      helpViewer = new JHelp(new HelpSet(cl, url));
    }