[英]Export .jar with .txt as resource won't work
I have a problem concerning the .jar Export. 我对.jar导出有问题。 I have to include a .txt file into my jar from where I read words into a list.
我必须在罐子中包含一个.txt文件,从那里我可以将单词读入列表中。 I used this to realize(in eclipse Run + Debug mode it works well):
我用它来实现(在Eclipse Run + Debug模式下效果很好):
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. 现在,如果我将其导出到JAR文件中,则该JAR文件应为可执行文件,并且该文件在资源中包含list.txt和.java文件(之所以这样,是因为它是家庭作业;-)),但是该程序的行为类似于没有文件。 I have the same problem when i use getResourceAsStream().
使用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.
我不明白,因为我两周前用另一个代码完成了此操作,并且它的工作原理是o_O。
What i have tried: 我试过的
My System is a Windows 7 x64 PC, Eclipse Juno and JRE7. 我的系统是Windows 7 x64 PC,Eclipse Juno和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"); URL url = Thread.currentThread()。getContextClassLoader()。getResource(“ resource / list.txt”);
File file = new File(url.getFile()); 文件文件=新文件(url.getFile());
You can't use java.io.File
to get the contents of a file inside of a jar. 您不能使用
java.io.File
来获取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
网址:jar:文件:/ C:/some/path/my.jar!/resource/wortliste.txt
File: file:\\C:\\some\\path\\my.jar!\\resource\\wortliste.txt
文件: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)
捕获到FileNotFoundException:file:\\ C:\\ some \\ path \\ my.jar!\\ resource \\ wortliste.txt(文件名,目录名或卷标语法不正确)
Instead, use Class.getClassLoader().getResourceAsStream(String)
to get an InputStream
for reading the file. 而是使用
Class.getClassLoader().getResourceAsStream(String)
获取用于读取文件的InputStream
。 If you still need to read the contents as a File
, you could stream the contents out to a temp file. 如果仍然需要将内容作为
File
读取,则可以将内容流式传输到临时文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.