0

I have a problem concerning the .jar Export. I have to include a .txt file into my jar from where I read words into a list. I used this to realize(in eclipse Run + Debug mode it works well):

File file = new File(Worte.class.getClassLoader().getResource("resource/wortliste.txt").getFile());

Now, if I export it into a JAR file, the JAR file is executable as it should, and it contains the list.txt and .java files in resource (as it should because it's homework ;-) ), but the program behaves like there was no file. I have the same problem when i use getResourceAsStream(). Does anybody know why this won't work? I don't understand that, because i did this 2 weeks ago with another code, and it worked o_O.

What i have tried:

  1. deleting the Project and import into a new one
  2. like 1, but into a new workspace

My System is a Windows 7 x64 PC, Eclipse Juno and JRE7.

Options I use for export:

[] Export generated class files and resources 
[x] export all output folders... 
[x] export java source files... 
[] export refactorings

[x] compress the contents... 
[x] add directory entries 
[x] overwrite existing files without warning


jar tvf ...
    39 Wed Jan 30 16:19:14 CET 2013 META-INF/MANIFEST.MF
     0 Wed Jan 30 00:34:16 CET 2013 resource/
100250 Wed Jan 30 00:37:24 CET 2013 resource/wortliste.txt
     0 Wed Jan 30 00:34:16 CET 2013 wortspiel/
  1291 Wed Jan 30 01:19:14 CET 2013 wortspiel/BuchstabenKollektion.java
  2251 Sun Jan 27 16:24:42 CET 2013 wortspiel/TestBuchstabenKollektion.java
   506 Sun Jan 27 17:38:48 CET 2013 wortspiel/UI.java
  1187 Sun Jan 27 16:24:42 CET 2013 wortspiel/TestWorte.java
  2932 Wed Jan 30 01:25:00 CET 2013 wortspiel/WortspielGUI.java
  4384 Wed Jan 30 01:50:40 CET 2013 wortspiel/Worte.java
   310 Sun Jan 27 16:25:08 CET 2013 .classpath
   383 Sun Jan 27 16:22:32 CET 2013 .project
  1992 Wed Jan 30 16:02:12 CET 2013 wortspiel/BuchstabenKollektion.class
  2075 Wed Jan 30 16:02:12 CET 2013 wortspiel/TestBuchstabenKollektion.class
  1104 Wed Jan 30 16:02:12 CET 2013 wortspiel/TestWorte.class
   942 Wed Jan 30 16:02:12 CET 2013 wortspiel/UI.class
  4701 Wed Jan 30 16:02:12 CET 2013 wortspiel/Worte.class
   688 Wed Jan 30 16:02:12 CET 2013 wortspiel/WortspielGUI$1.class
   688 Wed Jan 30 16:02:12 CET 2013 wortspiel/WortspielGUI$2.class
  3475 Wed Jan 30 16:02:12 CET 2013 wortspiel/WortspielGUI.class
tsmt
  • 3
  • 4
  • Can you give us a listing of the jar's contents? `jar tvf jarfilename` – flup Jan 30 '13 at 15:14
  • [This](http://stackoverflow.com/a/760926/13075) answer may help you. It explains how `getResource`works relative to the classpath of the class in which it is called. In my experience, there is a fair chance that this is where your problem originates. – Henrik Aasted Sørensen Jan 30 '13 at 15:14
  • Thanks Henrik for the link, but I also tried that :/ I updated the jar content @ flup – tsmt Jan 30 '13 at 15:25
  • Ther eis no `list.txt` file in jar. Only `worliste.txt`. – Mikita Belahlazau Jan 30 '13 at 15:28
  • yeah i changed the entry i put into stackoverflow :) in my code it's still wortliste.txt ;-) – tsmt Jan 30 '13 at 15:28

2 Answers2

0

Try this:

URL url = Thread.currentThread().getContextClassLoader().getResource("resource/list.txt");

File file = new File(url.getFile());

Cisco
  • 532
  • 1
  • 8
  • 24
  • Yes I also tried that, no change. But it also should be the same as my solution, doesn't it? – tsmt Jan 30 '13 at 15:26
  • Strange, It's a piece of code in my project and it works correctly. – Cisco Jan 30 '13 at 15:28
  • yeah as I mentioned in my post. I had this a few weeks ago and it works correct. Don't get that... – tsmt Jan 30 '13 at 15:34
  • Oh I've just seen you refer to Thread and not the Main Class. I tried, but it also fails after .jar export. In eclipse RUN/DEBUG it works :/ – tsmt Jan 30 '13 at 15:37
0

You can't use java.io.File to get the contents of a file inside of a jar. Try running the following snippet,

public class TestGetResource
{
    public static void main(String[] args) throws Exception
    {
        URL url = TestGetResource.class.getResource("/resource/wortliste.txt");
        System.out.println("URL: " + url);
        File f = new File(url.getFile());
        System.out.println("File: " + f);
        System.out.println("File exists: " + f.exists());
        
        try {
            FileReader reader = new FileReader(f);        
            reader.read();
        } catch (FileNotFoundException notFoundEx) {
            System.out.println("Caught FileNotFoundException: " + notFoundEx.getMessage());
        }
    }

}

You should see something similar to this,

URL: jar:file:/C:/some/path/my.jar!/resource/wortliste.txt

File: file:\C:\some\path\my.jar!\resource\wortliste.txt

File exists: false

Caught FileNotFoundException: file:\C:\some\path\my.jar!\resource\wortliste.txt (The filename, directory name, or volume label syntax is incorrect)

Instead, use Class.getClassLoader().getResourceAsStream(String) to get an InputStream for reading the file. If you still need to read the contents as a File, you could stream the contents out to a temp file.

Community
  • 1
  • 1
John McCarthy
  • 5,752
  • 2
  • 29
  • 40