Моя программа имеет следующий путь в файле .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
является локальным файлом, и я хочу относительный путь вместо абсолютного.
Спасибо.