4

I need to get relative path from my project to upload photos, however when I run System.getProperty("user.dir"); in my test, it returns /home/user/workspace/Myproject, but if I run it from my spring controller, it just returns /home/user.

Can anyone provide a insight that probably is happening?

aymeric
  • 3,877
  • 2
  • 28
  • 42
Danilo M.
  • 1,422
  • 5
  • 17
  • 31

1 Answers1

19

The user.dir property is the current working directory where you are running Java. You're starting java in /home/user/workspace/Myproject when you run your project but when Spring runs it, it's not guaranteed to be the same working directory. Are you looking for user.home (which should always be /home/user) instead?

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • No, I need to get the path project, regardless of computer the project is. – Danilo M. Sep 13 '12 at 20:16
  • @DaniloM. In that case, you should take a look at `getResource()` intsead. It allows you to read files inside your projects directory. Here's a question about that: http://stackoverflow.com/questions/3209901/absolute-path-of-projects-folder-in-java – Jon Lin Sep 13 '12 at 20:35
  • 3
    I solved my problem using `String path = request.getSession().getServletContext().getRealPath("/");`, which `request` is a `HttpServletRequest`. Thank you ! – Danilo M. Sep 13 '12 at 21:28