1

For example, if my project directory is /media/code/clojure/my-project/ and I have a file /media/code/clojure/my-project/src/foo.clj, how can I get "/media/code/clojure/my-project/" from foo.clj?

deadghost
  • 5,017
  • 3
  • 34
  • 46
  • 1
    Your question isn't clear. – Chiron Mar 27 '14 at 10:18
  • @Chiron Did the edit help? – deadghost Mar 27 '14 at 10:44
  • The truth is that there's no "project root". There's (class)path where code comes from, but no single directory standing for "root". What task are your trying to solve? – ffriend Mar 27 '14 at 11:39
  • @ffriend Defining a file upload directory. There's a very good shot I'm going about this in a totally wrong way. – deadghost Mar 27 '14 at 11:43
  • I always use `/tmp` (or corresponding user directory on Windows) for such things, since it's guaranteed to exist and have write permissions. If you really want it to be somewhere near the code, you can define some specific module/class with a path that is fixed w.r.t. your "root". E.g. you may have class `Main` in `root/src/Main.java`, get path to it as described [here](http://stackoverflow.com/questions/778187/getting-directory-path-to-class-file-containing-main) and just go up 1 level. – ffriend Mar 27 '14 at 11:55
  • @ffriend Right now uploaded files are going to `/tmp`, I meant "file upload directory" as in place where files are moved to from `/tmp`. I haven't had the chance to read up on whether it's a bad idea to skip `/tmp` entirely and just dump to my specified directory. – deadghost Mar 27 '14 at 12:11
  • Why not? Downloading directly to your destination directory is ok until you know how to recover in case of broken upload, but you need to clean such resources anyway. – ffriend Mar 27 '14 at 13:18

1 Answers1

2

This is unfortunately not possible because the idea of a "project root" is not well defined in the Java package model. There is a the idea of "the root of the source for this project" though once/if it's compiled into a jar file this concept no longer applies, the jar file could be loaded form anywhere on the system and that location will almost always (in the case of distributed software) be a system package installation directory. This would be an inappropriate place to download files to.

Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284