gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:18:33 25 4
gpt4 key购买 nike

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

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

最佳答案

这应该可以通过创建和使用您自己的类加载器来实现。您最有可能想要使用的候选类加载器是 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 的行,以根据共享目录获取您自己的 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));
}

关于JavaHelp:是否可以从类路径之外的位置加载帮助?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21236103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com