如何使用 .jar 文件之外的本地文件 setPage() JEditorPane?
我的程序在 .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< 中编译了程序/code> 文件。
file.createNewFile();
在 .jar
文件外部创建 temp.htm
文件
因此,当调用 editorPane.setPage(Program.class.getResource("temp.htm"));
时,在 test
包内搜索文件时找不到该文件.
如何setPage()
位于.jar
文件外部但与.jar位于同一文件夹中的
文件?temp.htm
文件
由于 temp.htm
是本地文件,我想要一个相对路径而不是绝对路径。
谢谢。
My program has the following path in the .jar filesrc/test/Program.class
and my program is as follow...
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());
}
}
The problem is that I have compiled the program in a .jar
file.
The file.createNewFile();
creates the temp.htm
file outside the .jar
file
So when editorPane.setPage(Program.class.getResource("temp.htm"));
is called the file is not found as it searches for file inside the test
package.
How to setPage()
the temp.htm
file whiich is outside the .jar
file but in the same folder as the .jar
file?
As the temp.htm
is a localfile and I want a relative path instead of an absolute path.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以尝试这样的事情:
You could try something like:
您不能使用 .java 文件作为类路径。您只能将路径(相对或绝对)或 JAR 文件放入类路径中。只要您写入临时文件的路径是类路径的一部分,它就应该
按照您编写的方式工作。
换句话说,在使用之前
需要确保
file
是类路径中列出的目录。You cannot use a .java-file as the classpath. You can only put paths (relative or absolute) or JAR-files into the classpath. As long as the path where you write the temporary file to is part of the classpath, it should work with
as you wrote.
In other words, before you use
you need to make sure that
file
is a directory listed in the classpath.