繁体   English   中英

Java Eclipse,导出jar。使用导入文件

[英]Java Eclipse, export jar. Using imported files

我创建的项目应该显示位于/src/files/pattern.xml中的文件“pattern.xml”。

当我在 eclipse 中运行代码时它工作正常。但是当我将项目导出到可执行文件 jar 并尝试执行它时,jar 不起作用。

也许问题在这里?

 URL dir_url = this.getClass().getClassLoader()
                .getResource("files\\pattern.xml");

这是代码。 有任何想法吗?

public class ResourseClass {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    test t = new test();
    t.Go();
    }
}

class test {

void Go() {
    try {

        URL dir_url = this.getClass().getClassLoader()
                .getResource("files\\pattern.xml");
        File dir = new File(dir_url.toURI());
        if (dir.exists()) {
            System.out.println("file exists");
        }

        FileReader frI = new FileReader(dir);
        BufferedReader br = new BufferedReader(frI);
        String text;
        String textout = "";

        while ((text = br.readLine()) != null) {
            textout = textout + text + "\n";
        }

        JOptionPane.showMessageDialog(null, textout, "Information",
                JOptionPane.WARNING_MESSAGE);
        br.close();

    } catch (FileNotFoundException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (URISyntaxException e3) {
        // TODO Auto-generated catch block
        e3.printStackTrace();
    }
}

    }

Package window

在此处输入图像描述

在eclipse正常执行

在此处输入图像描述

当代码在 eclipse 中执行时,它可以工作。

当您在 eclipse 之外运行它时,您需要将 pattern.xml 文件移动到与 class 文件相同的文件夹中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM