2014-01-20 53 views
0

我希望能夠從類路徑之外的自定義位置加載javahelp內容。此位置可能會在應用程序生命週期中更改,並且可能位於共享網絡設備上。JavaHelp:是否可以從類路徑之外的位置加載幫助?

不幸的是,HelpSet類期望一個類加載器,所以我猜我的helpset文件必須在類路徑中,或者有另一種方式嗎?提前致謝。

回答

1

這應該可以通過創建和使用您自己的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)); 
} 
+0

Thx,它的工作原理。請注意,URLClassLoader正在等待一組URL。 – Zardo

+0

@Zardo謝謝,我已經更新了答案,我還沒有編譯它,但現在看起來好多了。 –

相關問題