JavaHelp:是否可以从类路径外的位置加载帮助?

时间:2014-01-20 14:07:49

标签: java javahelp

我希望能够从类路径外的自定义位置加载javahelp内容。此位置可能会在应用程序生命周期内发生变化,并且可能位于共享网络设备上。

不幸的是,HelpSet类期待一个类加载器,所以我猜我的helpset文件必须在classpath中,还是有另一种方法?提前谢谢。

1 个答案:

答案 0 :(得分:2)

这应该可以通过创建和使用您自己的ClassLoader来实现。您最想要使用的候选ClassLoader是URLClassLoader

你可能有类似这样的代码:

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));
}

您需要更改使您的ClassLoader基于共享目录而不是基于系统类路径的行来获取您自己的行。所以像这样:

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));
}