5

Please explain me, why when i run this code, all is fine and i get parent directory of my classes:

URL dirUrl = PathsService.class.getResource("..");

and when I run this code:

URL dirUrl = PathsService.class.getResource("../..");

I get null in dirUrl.

I try like this: URL dirUrl = PathsService.class.getResource("..//.."); all the same I have a null in dirUrl.

How can I get parent/parent/parent ... directory in Java?

Boris Mitioglov
  • 1,092
  • 4
  • 16
  • 32

2 Answers2

12

NOTICE:

  1. As others have already stated, basing any functionality on the retrieval of some parent directory is a very bad design idea (and one that is almost certain to fail too).
  2. If you share more details about what you are trying to achieve (the big picture), someone could probably propose a better solution.

That said, you could try the following code:

import java.nio.file.*;
...
Path path = Paths.get(PathsService.class.getResource(".").toURI());
System.out.println(path.getParent());               // <-- Parent directory
System.out.println(path.getParent().getParent());   // <-- Parent of parent directory

Also note, that the above technic may work on your development environment, but may (and probably will) produce unexpected results when your application is "properly" deployed.

gkalpak
  • 47,844
  • 8
  • 105
  • 118
  • Please give me advice how to load my files then (if what i'm doing is bad design idea), if they are in my parent parent directory. – Boris Mitioglov May 27 '13 at 15:51
  • What exactly do you mean by "load my files" ? What kind of files ? And why do you need to load them manually ? (And what does "load" mean in this context ?) – gkalpak May 27 '13 at 16:09
  • I need to parse xml file which is in the "parent parent" directory – Boris Mitioglov May 27 '13 at 16:19
  • I think I incorrectly expressed when i said "load", better "get" – Boris Mitioglov May 27 '13 at 16:21
  • Will the .XML file be included your apps .JAR ? – gkalpak May 27 '13 at 16:25
  • Nope, I don't use JAR in my project. My project using BlazeDS to connect Java to Flex and classes from java should be in folder named "classes" on server. And Java classes parse ".XML" file which is located on server but in parent parent directory. Then information from parsing is sending to Flex. – Boris Mitioglov May 27 '13 at 19:12
  • 1
    1. If your class is in package `foo/bar` (alas `foo/bar/YourClass.class`) you can have access to all resources places in the this sub-tree, like that: e.g. `foo/res/demo.xml` can be accessed with `YourClass.class.getResource("/foo/res/demo.xml);` or `...("../res/demo.xml");`. If your resources are not in your package's subtree, but at a specific path relative to the system's root, you can access them directly using the absolute path (e.g. `/home/me/resources/demo.xml`. – gkalpak May 27 '13 at 19:45
  • 3. If you want the files places relative to the .class files, but not in the package subtree and IF this is an app for personal use only and in this specific setup, I guess you could use the solution I proposed above (if it works for you). In any case, since I have to experience with BlazeDS or Flex and the possible intricacies involved, I suggest you opened a new question, describing your setup and needs and let some more knowledgable (than me) fellow to give some advice about how yu could best handle it. – gkalpak May 27 '13 at 19:48
  • Yes, you are right. I think best solution for this problem it's relocate resource folder in package's subtree. – Boris Mitioglov May 27 '13 at 20:20
7

The result you are getting is perfectly right.

I think you have misunderstood the use of class.getResource.

Lets say you have package com.test and MyClass inside this package.

This MyClass.class.getResource(".") will give you the location of the file you are executing from. Means location of MyClass.

This MyClass.class.getResource("..") will go 1 level up in your package structure. So, this will return the location of directory test.

This MyClass.class.getResource("../..") will go further 1 level up. So, this will return the location of directory com.

Now, this MyClass.class.getResource("../../..") will attempt to go further 1 level up but since there is no package directory exists, this will return null.

So, class.getResource will not go out of the defined package structure and start accessing your computer directory. This is how this does not work. This only searches within your current class package structure.

Ravi Trivedi
  • 2,340
  • 2
  • 14
  • 20
  • 3
    Thanks for this answer. I am able to successfully get to the location of the `MyClass` using `MyClass.getClass().getResource(".")` while running the application within eclipse. However, when I deploy the app and run jar file, `getResource(".")` is returning null. Do you know a fix to this? – Joe Slater Jun 23 '14 at 17:14
  • Is there any **Error** ? – Ravi Trivedi Jun 30 '14 at 01:28