首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaHelp:是否可以从类路径之外的位置加载帮助?

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

Stack Overflow用户
提问于 2014-01-20 14:07:48
回答 1查看 160关注 0票数 0

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

坦率地说,HelpSet类需要一个类加载程序,所以我想我的帮助集文件必须在类路径中,还是有其他方法?提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-20 14:25:03

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

您可能有如下代码:

代码语言:javascript
复制
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的行,以根据共享目录获得自己的目录,而不是基于系统类路径。所以就像这样:

代码语言:javascript
复制
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));
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21236103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档