【发布时间】:2010-12-27 03:15:18
【问题描述】:
我的程序在.jar文件中有如下路径src/test/Program.class
我的程序如下...
Program.java
package test;
import java.io.File;
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
public class Program {
JEditorPane editorPane;
public Program() {
File file = new File("temp.htm");
try {
file.createNewFile();
editorPane = new JEditorPane();
editorPane.setPage(Program.class.getResource("temp.htm"));
} catch (IOException e) {
e.printStackTrace();
}
}
public JEditorPane getEditorPane(){
return editorPane;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
Program p = new Program();
frame.getContentPane().add(p.getEditorPane());
}
}
问题是我在.jar 文件中编译了程序。file.createNewFile(); 在.jar 文件外创建temp.htm 文件
因此,当调用 editorPane.setPage(Program.class.getResource("temp.htm")); 时,找不到文件,因为它在 test 包内搜索文件。
如何setPage()temp.htm 文件位于.jar 文件之外但与.jar 文件位于同一文件夹中?
由于temp.htm 是一个本地文件,我想要一个相对路径而不是绝对路径。
谢谢。
【问题讨论】:
标签: java jeditorpane