我的程序在 .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();
创建文件
所以当被调用时找不到文件,因为它在包内搜索文件。
如何在文件之外但与文件位于同一文件夹中的文件?
因为这是一个本地文件,我想要一个相对路径而不是绝对路径。 temp.htm
.jar
editorPane.setPage(Program.class.getResource("temp.htm"));
test
setPage()
temp.htm
.jar
.jar
temp.htm
谢谢。