6

I've got a little project in Spring. It's located (for example) in C:/users/projects/blog. How can I get this path?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Tony
  • 485
  • 2
  • 7
  • 13
  • A Java application has no knowledge of where it is. Only where it is started from, ie. which directory the `java` program is in (and even this is a hack and should not be used). – Sotirios Delimanolis Aug 14 '13 at 17:36
  • I've got a backup directory in my project. I want to add backups in there. – Tony Aug 14 '13 at 17:41
  • A backup of what? This is an [X Y problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Tell us what you want to do, we'll help you find a way to fix. Don't ask us what you want to do. – Sotirios Delimanolis Aug 14 '13 at 17:42
  • I know how to make backup. I just want to return project folder. – Tony Aug 14 '13 at 17:45
  • Return project folder from where? From a method with your application? I told you, that will only return the path to the `java` executable and I doubt that is also in your project's directory. – Sotirios Delimanolis Aug 14 '13 at 17:49
  • Refer to this regarding why user.dir doesnt work for you http://stackoverflow.com/questions/12413952/system-getpropertyuser-dir-does-not-work – vinay Aug 14 '13 at 18:05

3 Answers3

13

Maybe this what you looking for:

System.getProperty("user.dir")

This property returns user working directory for application.

aim
  • 1,461
  • 1
  • 13
  • 26
  • It shows me `C:\Users\xxx\springsource\sts-3.2.0.RELEASE`, but I need `C:/users/projects/blog` – Tony Aug 14 '13 at 17:57
  • It's seems that you run application from spring tools suite. Is it web application or you can run it from command line? – aim Aug 14 '13 at 18:04
  • It is web application. Yep. – Tony Aug 14 '13 at 18:05
  • So it this case your project files that located in `C:/users/projects/blog` are not associated to application when it launched on application container (tomcat for example). – aim Aug 14 '13 at 18:09
  • And so? How to get path to project? – Tony Aug 14 '13 at 18:13
  • You can't. They not related when you launch application on container. If you want get access to same files just add it to classpath or use ProperyPlaceHolder or use Spring MVC resources (if you want manage js/css resources). – aim Aug 14 '13 at 18:19
3

Java cannot determine the root of your project without projects meta data. You can get the absolute path to your current class executing the code using the following : YourClassName.class.getResource("").getPath()

vinay
  • 950
  • 1
  • 11
  • 27
  • Nope.`C:/Users/xxx/springsource/vfabric-tc-server-developer-2.8.2.RELEASE/base-instance/wtpwebapps/blog/WEB-INF/classes/net/babobka/blog/controller/` – Tony Aug 14 '13 at 18:32
  • If this is only for backup purposes, I would have a java code at project root with the follwing content and use the path for backup: public class DirectoryPath{ public static void main(String args[]){ System.out.println("Path::::"+DirectoryPath.class.getResource("").getPath()); } } – vinay Aug 14 '13 at 18:32
  • You get that because your java class is in package. You can try passing parameters to getResource("") and play with it. – vinay Aug 14 '13 at 18:36
0

Did you try using a properties file. here is an example

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>file:${path}</value>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

while running your code you will need to pass in -Dpath=C:\users\projects\blog as an argument

Ankit
  • 1,250
  • 16
  • 23