在我的应用程序中,我动态加载 JavaHelp 的 jhall 文件,因此我的代码使用反射。奇怪的是它在第一次调用时运行良好,而且我的 JavaHelp 屏幕显示良好,所有导航都显示出来。如果我关闭我的 JavaHelp 屏幕,然后再次打开它,我会收到以下消息:
UIDefaults.getUI() failed: createUI() failed for javax.help.JHelpContentViewer[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=,minimumSize=,preferredSize= ] java.lang.reflect.InvocationTargetException java.lang.Error
这发生在除第一个之外的所有 JavaHelp 请求上。但是,没有捕捉到异常,我尝试捕捉 createUI 错误,但也没有捕捉到任何东西(显然这并不奇怪)。这可能是一个类加载器问题——我对类加载器有点动摇——但没有发现异常……我真的需要一些帮助来解决这个错误。TIA
FWIW,(大部分)代码如下 - jHelpClass 和 helpSetClass 在其他地方声明......
jHelpClass = null;
helpSetClass = null;
URLClassLoader cl = null;
File jFile = new File(jhallJarFile);
if (!(jFile.exists())) {
JOptionPane.showMessageDialog(frame,"JavaHelp jar file shown in properties does not exist");
return;
}
try {
URI uri = jFile.toURI();
URL url = uri.toURL();
URL[] urls = new URL[]{url};
// Create a new class loader with the directory
cl = new URLClassLoader(urls, this.getClass().getClassLoader());
jHelpClass = cl.loadClass("javax.help.JHelp");
// Find the HelpSet file and create the HelpSet object
helpSetClass = cl.loadClass("javax.help.HelpSet");
} catch (MalformedURLException e2) {
} catch (ClassNotFoundException e2) {
} catch (NoClassDefFoundError e2) {
}
if (jHelpClass == null || helpSetClass == null) {
JOptionPane.showMessageDialog(frame,
"JHelp class or HelpSet class not found in jar file");
return;
}
// HelpSet hs = null;
URL url = null;
//Object hv = null;
JComponent hv = null;
try {
url = this.getClass().getClassLoader()
.getResource("helpSet.hs");
if (url == null) {
JOptionPane.showMessageDialog(frame, "HelpSet not found");
return;
}
Constructor conhs = helpSetClass.getConstructor(
ClassLoader.class, URL.class);
//Object hs = conhs.newInstance(null, url);
Object hs = conhs.newInstance(cl, url);
Constructor conjh = jHelpClass.getConstructor(helpSetClass);
hv = (JComponent) conjh.newInstance(hs);
} catch (Exception e2) {
JOptionPane.showMessageDialog(frame,
"HelpSet could not be processed: " + e2);
return;
}
// Create a new frame.
final JFrame frame2 = new JFrame();
frame2.setTitle("Help DrawFBP");
frame2.setIconImage(favicon.getImage());
//frame2.setRequestFocusEnabled(true);
frame2.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ev) {
if (ev.getKeyCode() == KeyEvent.VK_ESCAPE) {
//frame2.setVisible(false);
frame2.dispose();
}
}
});
// Set its size.
frame2.setPreferredSize(frame.getPreferredSize());
// Add the created helpViewer to it.
frame2.getContentPane().add(hv);code
// Set a default close operation.
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// Make the frame visible.
frame2.setVisible(true);
frame2.pack();
return;