我正在尝试将存储在 jar 文件中的 html 页面加载到帮助 JEditorPane 中。到目前为止,当我在 eclipse 中运行它时它可以工作,但是当我制作一个可运行的 jar 时它不会工作,除非我将 map res/pages/... 与 jar 文件放在同一个 map 中
class HelpButtonHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
infodex = new JEditorPane();
helpDialog = new JDialog();
URL url1 = null;
try {
url1 = (new java.io.File("res/pages/help.html")).toURI().toURL();
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
infodex.setPage(url1);
} catch (IOException e) {
e.printStackTrace();
}
helpDialog.getContentPane().add(new JScrollPane(infodex));
helpDialog.setBounds(400,200,700,600);
helpDialog.show();
infodex.setEditable(false);
Hyperactive hyper = new Hyperactive();
infodex.addHyperlinkListener(hyper);
}
}
最佳答案
打包在 .jar 中的文件不是文件系统上的文件。您不能使用 File 类访问它。
.jar 中的文件称为应用程序资源。您可以使用 Class.getResource 访问它方法:
url1 = HelpButtonHandler.class.getResource("/res/pages/help.html");
确保文件正确打包到您的 .jar 中取决于您。如果 url1
为空,请检查 .jar 文件的结构。
关于java - JEditorPane 中的 jar 文件加载 html 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19587066/