JavaHelp的Java newInstance调用在第二次和后续调用中因createUI错误而崩溃
在我的应用程序中,我动态加载jhall文件以获取JavaHelp,因此我的代码使用反射。奇怪的是,它在第一次调用时运行良好,我的JavaHelp屏幕显示良好,并且具有所有导航功能。如果我关闭JavaHelp屏幕,然后再次打开它,我会收到以下消息: UIDefaults.getUI()失败:javax.help.JHelpContentViewer[,0,0,0x0,无效,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=,minimumSize=,preferredSize=]java.lang.reflect.InvocationTargetException的createUI()失败 java.lang.Error 除了第一个JavaHelp请求之外,所有JavaHelp请求都会发生这种情况。但是,没有捕获到异常,我尝试捕获createUI错误,但也没有捕获任何内容(显然这并不奇怪)。这可能是类加载器的问题-我对类加载器有点不确定-但是没有发现异常。。。我真的需要一些帮助来解决这个错误。短暂性脑缺血发作 FWIW,(大部分)代码如下-jHelpClass和helpSetClass在别处声明JavaHelp的Java newInstance调用在第二次和后续调用中因createUI错误而崩溃,java,classloader,javahelp,Java,Classloader,Javahelp,在我的应用程序中,我动态加载jhall文件以获取JavaHelp,因此我的代码使用反射。奇怪的是,它在第一次调用时运行良好,我的JavaHelp屏幕显示良好,并且具有所有导航功能。如果我关闭JavaHelp屏幕,然后再次打开它,我会收到以下消息: UIDefaults.getUI()失败:javax.help.JHelpContentViewer[,0,0,0x0,无效,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=,mini
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实例)的变量来解决这个问题,而不是每次都重新计算它-显然所有的重新加载都会以某种方式损坏存储中的类。。。?不知道这是否有意义,但我的应用程序目前正在运行。我很想知道到底是什么导致了这种情况,如果将来有技术来解决它,但显然解决方案不再紧迫!感谢所有花时间在这上面的人