Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find context path in normal Java class

Tags:

java

path

I want to find context path of my web Application in normal Java class.If I find I can specify paths like this /Rod1/thermalMap.exe wherever I need.

I know, How to find in servlet using the following code

   getServletContext().getRealPath("");

My webApps folder in the following way.

image

like image 267
Hanumath Avatar asked Sep 17 '13 09:09

Hanumath


2 Answers

You can get the absolute path to to your webApp/WEB-INF/classes directory as below:

URL resource = getClass().getResource("/");
String path = resource.getPath();

This will return you an absolute path like this:

/C:/SERVERS/x/y/x/yourApp/WEB-INF/classes

And from this you can get the path to the yourApp directory:

path = path.replace("WEB-INF/classes/", "");

which you can use to specify paths like /Rod1/thermalMap.exe, by appending to this path.

like image 174
Debojit Saikia Avatar answered Sep 22 '22 20:09

Debojit Saikia


Have you try this?

 String path = new File(".").getCanonicalPath();
like image 33
ZaoTaoBao Avatar answered Sep 22 '22 20:09

ZaoTaoBao