简体   繁体   中英

Export .jar with .txt as resource won't work

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

Try this:

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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