0

I have created project that should display file "pattern.xml" which is located in /src/files/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.

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

enter image description here

Normal execution in eclipse

enter image description here

When code is executed in eclipse it works.

icedwater
  • 4,701
  • 3
  • 35
  • 50
  • I'm not entirely sure it's being compiled into the jar. Can you check if the file is present inside the .jar archive? –  Apr 08 '12 at 16:39
  • yes, when i open the archive the file "pattern.xml" is inside the .jar archive. – Леха Шевченко Apr 08 '12 at 20:22
  • Shouldn't it be in .jar/files/ ? try making a files folder in the jar, moving the xml there and running the jar. –  Apr 08 '12 at 20:39
  • http://dl.dropbox.com/u/12451869/Pattern_printer_example.jar this is my jar. File "pattern.xml" is in .jar/files/ – Леха Шевченко Apr 08 '12 at 20:53
  • try using single slash, java is cross platform and gets consed by `\\`, try making the line `URL dir_url = this.getClass().getClassLoader() .getResource("files/pattern.xml");` –  Apr 08 '12 at 22:02
  • If it works, say so and I'll post as answer :) –  Apr 08 '12 at 22:03
  • i`ve done as you said to me, but it still not working =( The same problem. It works fine in eclipse, but doesnt execute jar – Леха Шевченко Apr 09 '12 at 17:23
  • Have you tried `getResourceAsStream`? Look into that, it seems to bug somewhat less. –  Apr 10 '12 at 00:07
  • Possible duplicate of [Reading a resource file from within jar](http://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar) – GreenThor Aug 18 '16 at 11:12

1 Answers1

0

You'll want to move the pattern.xml file to the same folder as your class files when you run it outside of eclipse.

Wookie1120
  • 194
  • 1
  • 11