It is quite hard to explain what I mean here, but...

Say I have a class that is stored on a hard drive like this..


i.e. the class MyToolKit is stored in the package project.helpers and would declare it in its code like this..

package project.helpers;

public class MyToolkit {
 // .... Code etc here.

Say for instance this is stored on the hard drive in the location "C:\MyJavaProjects\MyNewProject\project\helpers\MyToolkit.class" i.e. the base location of the project would be "C:\MyJavaProjects\MyNewProject", where I would like to store things the program can use such as images or configuration files or whatever else.

What I want to know is, from the code of MyToolkit.class i.e. how to find out the base location of the project. I'm aware this could really be done by jumping back 2 directories, since I know MyToolkit is in the package project.helpers, but this is just hard coding from what I already know. I am looking for a method of doing it so that no matter what package, and how many sub-packages MyToolkit could find itself in, how the base location of the whole project can be retrieved. so in the above example I would like a method inside MyToolkit class that can simply work out where the base location is on the actual hard drive, so that method would return the string "C:\MyJavaProjects\MyNewProject".

In the end I just want to keep my program and resources seperate, and therefore need a way of find my way back to the root.

Its been really hard to try to explain it, sorry if it isn't clear.


When the whole thing is done, I intend to jar the whole program up and what I really need is the full path name of the directory the .jar is stored. Can anyone suggest a way of doing this?


Try System.getProperty("user.dir")

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.