简体   繁体   English

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

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

I have created project that should display file "pattern.xml" which is located in /src/files/pattern.xml .我创建的项目应该显示位于/src/files/pattern.xml中的文件“pattern.xml”。

It works fine when I run code in eclipse. But when I export project to executable jar and try to execute it, jar doesn't work.当我在 eclipse 中运行代码时它工作正常。但是当我将项目导出到可执行文件 jar 并尝试执行它时,jar 不起作用。

Maybe problem is here?也许问题在这里?

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

Here is code.这是代码。 Any ideas?有任何想法吗?

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 Package window

在此处输入图像描述

Normal execution in eclipse在eclipse正常执行

在此处输入图像描述

When code is executed in eclipse it works.当代码在 eclipse 中执行时,它可以工作。

You'll want to move the pattern.xml file to the same folder as your class files when you run it outside of eclipse.当您在 eclipse 之外运行它时,您需要将 pattern.xml 文件移动到与 class 文件相同的文件夹中。

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

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