How to get the real path of Java application at runtime?

Try;

String path = new File(".").getCanonicalPath();

And what about using this.getClass().getProtectionDomain().getCodeSource().getLocation()?


It isn't clear what you're asking for. I don't know what 'with respect to the web application we are using' means if getServletContext().getRealPath() isn't the answer, but:

  • The current user's current working directory is given by System.getProperty("user.dir")
  • The current user's home directory is given by System.getProperty("user.home")
  • The location of the JAR file from which the current class was loaded is given by this.getClass().getProtectionDomain().getCodeSource().getLocation().

Tags:

Java

Filepath