HOW TO: Read a file from jar and war files (java and webapp archive) ?

19 octobre 2008 at 17 h 01 min 4 commentaires

As many java programmers, i spent hours searching for the « good recipe » to read a txt file from my java archive to configure an application. The config file aren’t available to final users, so it should only help the programmer to configure his webapp, without having an external file / URL to check.

Reading a java archive would generally be done using the classloader of the JVM.

How to read from a jar file

The files you are looking to must be located in a directory named « classes » at the top of your src folder.
Unless your file exists, you will be able to open it as an URL or as a stream, using the application classloader.
To access to the class loader, just use your class, get the static  »class » instance pointer, and invoke getClassLoader method.
Then, use one of these two method:
getResourceAsStream() to have an input file
getResource() to have an URL object

If you try to open a file outside the classes folder, the classloader will throw a NullException.

InputStream in =
new InputStreamReader(FileLoader.class.getClassLoader().getResourceAsStream("file.txt") );

How to read from a war file

Here is what the tomcat documentation says about loading your own files shipped within your webapp archive:

WebappX – A class loader is created for each web application that is deployed in a single Tomcat 6 instance. All unpacked classes and resources in the /WEB-INF/classes directory of your web application archive, plus classes and resources in JAR files under the /WEB-INF/lib directory of your web application archive, are made visible to the containing web application, but to no others.

So it means you will use the /WEB-INF/classes directory. It means the code would be nearly the same as for a Desktop application jar file. Get a classloader from the classe you are coding (just change the name of it) and get your file.

The best way to look to a config file from a webapp is to implement the servlet method init().
Let’ try using the same code
InputStream in =
new InputStreamReader(FileLoaderServlet.class.getClassLoader().getResourceAsStream("file.txt") );

It works fine !

So just remember to put your into the right folder if you are getting an exception.

Reading files outside the classes folder ?

Theses articles will show you a simple way to achieve it for jar files:

For war files, don’t use the servlet container’s classloader, but use the ServletContext instead.

This method allows servlet containers to make a resource available to a servlet from any location, without using a class loader.

ServletContext context = getServletContext();
InputStream is = context.getResourceAsStream("/yourfilename.txt");

It is recommended to keep it under the /WEB-INF directory if you don’t want browers being able to access it.

The path must begin with a « / » and is interpreted as relative to the current context root. This method returns null if no resource exists at the specified path. For example ServletContext.getResourceAsStream(« WEB-INF/resources/yourfilename.cnf ») will return a nul exception, so be careful !

Happy coding.


Entry filed under: english publications, java. Tags: , , , , , .

Comment convertir un fichier ISO en UTF-8 ? margin-left:auto et margin-right:auto : problème sous IE 7 ?

4 commentaires Add your own

  • 1. Jan Ole  |  30 novembre 2008 à 20 h 49 min

    Hi. I have great use of this article, but do you know how to, or if it is possible, to write to a file in the jar whitout unpacking and packing again?

  • 2. Amit  |  24 février 2009 à 22 h 18 min

    it was really helpful and simple.Thanks.

    • 3. Radhesh  |  7 octobre 2009 à 11 h 11 min

      it was of decent help..

  • 4. Radhesh  |  7 octobre 2009 à 11 h 11 min

    It was of decent help to me too..


Laisser un commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:


Vous commentez à l'aide de votre compte Déconnexion /  Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion /  Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion /  Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion /  Changer )


Connexion à %s

Trackback this post  |  Subscribe to the comments via RSS Feed

%d blogueurs aiment cette page :