如何从tomcat7中的jar获取txt文件

如何从tomcat7中的jar获取txt文件

问题描述:

我有 txt 文件。我使用 jar cf0 ... 将它打包成拉链和罐子。为了让这个工作起作用,我尝试了两种方法。

I have a txt file. I have packaged it in a zip and a jar, using jar cf0.... I have tried both in an effort to get this to work.

models.zip/models.jar
 |
 |_subfolder
     |
     |_MyFile.txt

在某些代码中,我有......

In some code I have...

InputStream in = getClass().getClassLoader().getResourceAsStream(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));

我试过设置路径...

I have tried setting path to...

MyFile.txt
/MyFile.txt
/subfolder/MyFile.txt
classpath:/subfolder/MyFile.txt
models.zip/subfolder/MyFile.txt
models.jar/subfolder/MyFile.txt
models.zip!/subfolder/MyFile.txt
models.jar!/subfolder/MyFile.txt

我也试过把 jar / zip WEB-INF / classes WEB-INF / lib

这些都不起作用,中的始终为空。

None of this works, in is always null.

只是你知道,当我把MyFile.txt放在WEB-INF / classes中我可以搞定。

And just so you know, when I put MyFile.txt in WEB-INF/classes I can get it fine.

请提供经过验证的答案!请不要只是复制并粘贴其他答案中的代码。

Please provide a tried and tested answer! Please dont just copy and paste code from another answer.

我觉得自己像个笨蛋!问题是在lib和classes文件夹中都有models.jar。它应该只在lib文件夹中。

I feel like a plonker! Problem was having models.jar in both the lib AND the classes folder. It should be in just the lib folder.