TedTrippin
TedTrippin

Reputation: 3667

How to get a txt file from a jar in tomcat7

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

I have also tried putting the jar/zip in WEB-INF/classes and WEB-INF/lib.

None of this works, in is always null.

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.

Upvotes: 2

Views: 228

Answers (3)

TedTrippin
TedTrippin

Reputation: 3667

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.

Upvotes: 1

ElderMael
ElderMael

Reputation: 7111

I think the problem is the trailing / in /subfolder/MyFile.txt.

The spring ClassPathResource class uses almost the same operation you are using, but the javadoc states plainly:

Create a new ClassPathResource for ClassLoader usage. A leading slash will be removed, as the ClassLoader resource access methods will not accept it.

So, without spring it would be:

Thread.currentThread().getContextClassLoader().getResourceAsStream("subfolder/M‌​yFile.txt");

Also, your jar must be put in WEB-INF/lib or if you want to put the text file alone, put it in WEB-INF/classes/subfolder/.

If you are using spring (my guess because you are using the pseudo url classpath:) is use directly this:

InputStream is = new ClassPathResource("subfolder/MyFile.txt").getInputStream();

Upvotes: 1

BigMike
BigMike

Reputation: 6873

Try with

InputStream is = getClass().getClassLoader().getResourceAsStream  ("/subfolder/MyFile.txt");

and put your jar file in WEB-INF/lib/

EDIT: you should trust your own code, this is a fragment from a running webapp of mine. Class is in package "it.sft.utils.props" and the relative property file is packaged via ant int the very same folder of the very same jar. This works flawlessly.

In your specific scenario, probably you don't have the jar in your classpath

 private void loadFromProperties() throws IOException {
            if (currencies == null) {
                currencies = new Properties();
                InputStream is = this.getClass().getClassLoader().getResourceAsStream  ("/it/sft/utils/props/currencies.props");
            if (is != null) {

                currencies.load(is );

                for (String key : currencies.stringPropertyNames()) {
                    String value = currencies.getProperty(key);
                    try {
                        String [] tmp = value.split(",");
                        String code = tmp[0];
                        int decs = Integer.parseInt(tmp[1]);
                        String text = tmp[2];
                        Logger.getLogger(CurrencyResolver.class).debug("Adding " + key + " with code " + code + " [" + text + "]");
                        storeValutaIso4217(new ValutaIso4217(key, code, decs, text));
                    }catch (Exception e) {
                        // Ignore
                        Logger.getLogger(CurrencyResolver.class).warn("Cannot handle " + key + " >" + value + "<");
                    }
                }
            }
        }
    }

Or are you by chance running it from some kind of IDEs (NetBeans or Eclipse) ?

Just for being paranoid: the jar file is actually readable by the tomcat run user, isn't it ?

Upvotes: 2

Related Questions