0

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

不幸的是,HelpSet 类需要一个类加载器,所以我猜我的帮助集文件必须在类路径中,还是有其他方法?提前致谢。

4

1 回答 1

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));
}
于 2014-01-20T14:25:03.443 回答