Relative path to the file with the system property "user.dir"

I am trying to read in a local file in my web application on a server. When you test this outside of your JBoss deployment, the file path is correct. However, when I deploy it to JBoss, the file path changes the JBoss directory bin. A systemic property user.dirchanges when it unfolds like a war.

How to prevent this without using an absolute file path?

I want to avoid the absolute path to the file, because the project must be replicated on several machines with 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)));
+4
source share
2 answers

You tried to call from

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

It gives a real path to the current operating system.

Hello,

FT

0

. , , . src/main/resources - , .

src/main/resources , :

:

String file = "/flat.json";

JsonReader reader = new JsonReader(new InputStreamReader(getClass().getResourceAsStream(file)));
0

Source: https://habr.com/ru/post/1609004/


All Articles