Java newInstance调用JavaHelp在第二次和后续调用时崩溃,并出现createUI错误
在我的应用程序中,我动态加载JavaHelp的jhall文件,所以我的代码使用反射。奇怪的是它在第一次调用时可以正常工作,并且我的JavaHelp屏幕显示正常,具有所有导航。如果我闭上JavaHelp的屏幕,并再次打开它,我得到的消息:Java newInstance调用JavaHelp在第二次和后续调用时崩溃,并出现createUI错误
UIDefaults.getUI()失败:createUI中()失败javax.help.JHelpContentViewer [,0,0,0x0,无效的, alignmentX = 0.0,alignmentY = 0.0,边界=,标志= 0,MAXIMUMSIZE =,=的minimumSize,首选大小=] 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;
证明,我可以挂到所谓的“HV”变量解决这个问题(JHelp实例),而不是每次都重新计算它 - 显然,所有重装都会以某种方式损坏存储中的类...?不知道这是否有意义,但它是我的应用程序正在工作。我有兴趣了解更多关于究竟是什么造成了这种情况,以及是否有技术来解决未来的问题,但显然解决方案已不再紧迫!感谢任何花时间在此的人。