WolfmanDragon
WolfmanDragon

Reputation: 7952

How a servlet can get the absolute path to a file outside of the servlet?

We have been using System.getProperties("user.dir") to get the location of a properties file. Now that it has been deployed on Tomcat(via servlet), the System call is giving the location as tomcat and not at the location at where the properties file exist.

How can we call the the properties file dynamically?

Given:

Upvotes: 3

Views: 35355

Answers (9)

JosefB
JosefB

Reputation: 99

We had the same requirements. The simplest thing that worked so far was to just store a basedir property in one of the property files we were already loading. Then define a method like getExternalDocPath(String path).

This allowed us to extend the docbase of Tomcat (which only Tomcat 7 supports?). Out on the web someone posted a class that actually extends Tomcat's docbase to allow multiple paths, i.e., "alpha;baker;charlie".

Upvotes: 0

david a.
david a.

Reputation: 5291

If I understood the question correclty, another alternative might be to call ServletContext.getRealPath() on a jsp or a static file in your app and derive the path from the result.

It implies webapp deployed as "expanded" - i.e. not as a compressed war file - but that is what most appservers do anyway.

Upvotes: 1

toolkit
toolkit

Reputation: 50297

You could store the location of your properties file in JNDI?

This should be portable across Tomcat, and for Java EE Application Servers.

Upvotes: 0

You must have a way of knowing the path of the property file which you can then wrap in a File and pass to the load() method of your properties object.

If you run inside a Tomcat service you are not running as the user you installed it as,so you cannot derive the home directory. You most likely need to hardcode SOMETHING then.


Edit: The property file is relative to the application. See http://www.exampledepot.com/egs/java.lang/ClassOrigin.html for an example of how to get to the file name for the bytecode for a given class. You should be able to continue from there.

Class cls = this.getClass();
ProtectionDomain pDomain = cls.getProtectionDomain();
CodeSource cSource = pDomain.getCodeSource();
URL loc = cSource.getLocation();  // file:/c:/almanac14/examples/

You should be aware that some security managers disallow this.

Upvotes: 2

Rocket Surgeon
Rocket Surgeon

Reputation: 575

Another approach may be to pass the variable value to the JVM with -D parameter. That way you can tie your code to the same variable name and then pass different values at the time of start-up. I haven't tried this, but i'm thinking it should work for an app deployed in Tomcat as well, if you modify startCatalina script to pass -D parameter to JVM

Upvotes: 0

Akrikos
Akrikos

Reputation: 3662

I think ClassName.class.getResourceAsStream() will work for you. The comments on that method point you at ClassLoader.getResource(), which tells you how to specify a file in your classpath.

Something like this should work:

InputStream foo = ClassName.class.getResourceAsStream("file.name");

Where file.name is at the base of your classpath somewhere. If file.name is in the com.foo.bar package, you would use "/com/foo/bar/file.name"

Upvotes: 1

Akrikos
Akrikos

Reputation: 3662

Is it possible to put the config file within the classpath and reference it via something like springs ClassPathResource?

You should be able to use it like this:

ClassPathResource foo = new ClassPathResource("file.name");

Where file.name exists somewhere in the classpath at the lowest level such as:

  • /webapps/WEB-INF/classes/
  • the base of one of your jar files

Upvotes: 0

neesh
neesh

Reputation: 5275

You can place your property files in your class path and read them as an input stream using something like:

ClassName.class.getResourceAsStream(filePath + fileName);

Where filePath is the relative path of the file from the class path root and fileName is the name of the file.

If you need to get the absolute path to a file you read as a resource, you can do some thing like this:

ClassName.class.getResource(filePath + fileName).getPath()

Upvotes: 0

John Topley
John Topley

Reputation: 115432

Take a look at ServletContext's getResource and getResourceAsStream methods.

Upvotes: 2

Related Questions