Absolute path for the class at runtime.

Hi All,

It is required to get the absolute path of the current directory where my class is residing. This has to happen during runtime inside application server.

Following solutions in the internet were not useful for me.
URL classFileDir = new TestAnything().getClass().getResource("./");
File currDir = new File(".");
System.out.println ( "Dir : " + currDir );
System.out.println ( "Dir : " + currDir.getCanonicalPath() );

Any other idea?

Best Regards
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Loganathan NatarajanLAMP DeveloperCommented:
if you are using servlet class,


ServletContext context = session.getServletContext();
String realContextPath = context.getRealPath(request.getContextPath());
inditrozenAuthor Commented:
Thx for reply
I do not have session available in this class.
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

inditrozenAuthor Commented:
I wanted to find the dir path for file.ext as following directory structure

Here is the folder structure.

This is how I solved it.
URL url = SameDirClass.class.getResource("file.ext");
File file = new File(url.getPath());
return file.getParent()+File.separator;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
inditrozenAuthor Commented:
no obj
Closed, 500 points refunded.
Community Support Moderator
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Applications

From novice to tech pro — start learning today.