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

Solution 1:

Try;

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

Solution 2:

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().

Solution 3:

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