4

I'm trying to read in a local file in my web application on the server. When testing this outside of the deployment to JBoss, the file path is correct. However, when I deploy it to JBoss the file path changes JBoss's bin directory. The user.dir system property changes when it's deployed as a war.

How do I prevent that without using an absolute file path?

I want to avoid an absolute file path, because the project needs to be replicated on multiple machines that have different directory structures.

String curDir = System.getProperty("user.dir");

String fileLocation = curDir.toString() + "/end/of/path/to/flat.json";
JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream(fileLocation)));
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
UltraSonja
  • 881
  • 1
  • 20
  • 33
  • 5
    In a servlet environment, you should not use relative paths from the user dir. – meskobalazs Sep 25 '15 at 14:22
  • 2
    @meskobalazs I read a little about classpath resources. Is that something I should look into more? – UltraSonja Sep 25 '15 at 14:23
  • 1
    That would be a better idea. – meskobalazs Sep 25 '15 at 14:24
  • @UltraSonja If you have different machines with different structures, you should have some configuration setting which sets your base directory and make all paths relative to this. This ensures you maimal flexibility in all directions. – glglgl Sep 25 '15 at 14:24
  • If push comes to shove and you must have your files in the file system, you can use a servlet init parameter or similar mechanism to pass the path of an external directory. Relying on current directory or `user.dir` is a bad idea in a web app. – biziclop Sep 25 '15 at 14:27
  • Is it a temp file that is needed only for the duration of the app being up? Or does it need to persist even when the app is not running? – Jose Martinez Sep 25 '15 at 14:50
  • @JoseMartinez For now it persists when the app is not running... the app is still under development. I have a working solution for now... will post it in a sec. – UltraSonja Sep 25 '15 at 14:53

2 Answers2

0

Have you tried to call from the

HttpServeltRequest.getServletContext().getRealPath("META-INT/aop.xml");

It give you the real path on the current operating system.

Regards,

FT

fabien t
  • 358
  • 1
  • 9
0

Thanks to this question and answer I have found a solution. I'm not sure if this is the best way of doing it, but it works. I added src/main/resources folder in my web app, and put the files I need to access there.

Resources from src/main/resources will be put onto the root of the classpath, so you'll need to get the resource as:

The code:

String file = "/flat.json";

JsonReader reader = new JsonReader(new InputStreamReader(getClass().getResourceAsStream(file)));
Community
  • 1
  • 1
UltraSonja
  • 881
  • 1
  • 20
  • 33
  • 1
    That's for files that are created at or before build time when the WAR is created. Do not add to resources once it is deployed. – Jose Martinez Sep 25 '15 at 15:07
  • @JoseMartinez Then what method would I use for files that aren't created at or before build time? In a real-time environment the the files are going to be generated periodically. – UltraSonja Sep 25 '15 at 15:10
  • If the files are only needed temporarily, then use tmp.dir; good for cache. If the files are needed between deployments on same container instance (e.g. JBOSS installed in a directory), then consider using relative path. The relative path will live beyond repeated deployments, whereas resource folder is re-deployed with each deployment. If the files needs to exist across various containers and the app may be installed in different directories, then do what DB applications due, they have a designated DATA directory that is absolute but can be overwritten using an env-variable or system-prop. – Jose Martinez Sep 25 '15 at 15:19
  • For doing this, you need to have the good rights on the targeted Operating System for read the file. It will depend the constraints of your production environnement. But generally a server is setup with a specific user who have only right on it installation folder, and these webApps nothing more is need for security purpose. As i said in my answers, if you want access a file that is located in your webApp, use the **ServletContext** as well who give you the real path of the webApp. – fabien t Sep 27 '15 at 09:54