如何使用 .jar 文件之外的本地文件 setPage() JEditorPane?

发布于 2024-10-09 16:26:36 字数 1456 浏览 0 评论 0原文

我的程序在 .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 file
src/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

八巷 2024-10-16 16:26:36

你可以尝试这样的事情:

// get location of the code source
URL url = yourpackage.Main.class.getProtectionDomain().getCodeSource().getLocation();

try {
    // extract directory from code source url
    String root = (new File(url.toURI())).getParentFile().getPath();
    File doc = new File(root, "test.htm");
    // create htm file contents for testing
    FileWriter writer = new FileWriter(doc);
    writer.write("<h1>Test</h1>");
    writer.close();
    // open it in the editor
    editor.setPage(doc.toURI().toURL());
} catch (Exception e) {
    e.printStackTrace();
}

You could try something like:

// get location of the code source
URL url = yourpackage.Main.class.getProtectionDomain().getCodeSource().getLocation();

try {
    // extract directory from code source url
    String root = (new File(url.toURI())).getParentFile().getPath();
    File doc = new File(root, "test.htm");
    // create htm file contents for testing
    FileWriter writer = new FileWriter(doc);
    writer.write("<h1>Test</h1>");
    writer.close();
    // open it in the editor
    editor.setPage(doc.toURI().toURL());
} catch (Exception e) {
    e.printStackTrace();
}
溺深海 2024-10-16 16:26:36

您不能使用 .java 文件作为类路径。您只能将路径(相对或绝对)或 JAR 文件放入类路径中。只要您写入临时文件的路径是类路径的一部分,它就应该

editorPane.setPage(Program.class.getResource("temp.htm"));

按照您编写的方式工作。

换句话说,在使用之前

file.createNewFile();

需要确保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

editorPane.setPage(Program.class.getResource("temp.htm"));

as you wrote.

In other words, before you use

file.createNewFile();

you need to make sure that file is a directory listed in the classpath.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文