1

I have a maven web project in eclipse. I need to get the project's path, actually have to get list of files under src\main\resources\someFolder in project.

Tried String dataDir = "src\\main\\resources\\someFolder";, on running this directory structure is created inside eclipse folder like F:\softwares\eclipse\eclipse\src\main\resources\someFolder. Same when using / instead of \\.

Tried System.getProperty("user.dir")and new File(".").getAbsolutePath(), they return F:\softwares\eclipse\eclipse.

I need to access the project folder in my workspace F:\workspace\Project\src\main\resources\someFolder

But when created a core java app and used System.getProperty("user.dir")and new File(".").getAbsolutePath(), I am getting project path in workspace, F:\workspace\Project. This src\\main\\resources\\someFolder also works fine then.

Why this odd behavior from eclipse?

Abhay
  • 314
  • 1
  • 2
  • 11

1 Answers1

0

As mentioned here the directory user.dir is the place where the JVM is started. As web applications are mostly jar/war/ear packages placed somewhere within the folder of the server eclipse handles them in a different way because the behaviour of such a web application is different. You cannot expect to have file access from outside the jar/war/ear file. Within the jar/war/ear file everything from within src/main/resources will be available just by using getResourceAsStream as described in many other stackoverflow articles. This way you mustn't use src/main/resources/myfile.txt but myfile.txt.

Don't try to guess or use what the user.dir / JVM/server start folder is!

Community
  • 1
  • 1
Niklas P
  • 3,427
  • 2
  • 15
  • 19