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

Viewed 191191

I am creating a Java application where I am using log4j. I have given the absolute path of configuration log4j file and also an absolute path of generated log file(where this log file are generated). I can get the absolute path of a Java web application at run time via:

String prefix =  getServletContext().getRealPath("/");

but in the context of a normal Java application, what can we use?

15 Answers

If you want to get the real path of java web application such as Spring (Servlet), you can get it from Servlet Context object that comes with your HttpServletRequest.

@GetMapping("/")
public String index(ModelMap m, HttpServletRequest request) {
    String realPath = request.getServletContext().getRealPath("/");
    System.out.println(realPath);
    return "index";
}

I think everyone's missing a key problem with this.

String prefix =  getServletContext().getRealPath("/");

The servlet instance could be on one of the arbitrarily many nodes and doesn't technically require a file system at all.

In Java EE containers for example the application could be loaded from a database or even a directory server. Different parts of the application can also be running on different nodes. Access to the world outside the application is provided by the application server.

Use java.util.logging or Apache Commons Logging if you have to maintain compatibility with legacy log4j. Tell the application server where the log file is supposed to go.

I think System.getProperty("java.class.path") and System.getProperty("path.separator") are better schemes. eg:

private final static String JAR_SUFFIX = ".jar";

public static String getJarRuntimePath(){
        String javaClassPath = System.getProperty("java.class.path");
        String pathSeparator = System.getProperty("path.separator");
        if(javaClassPath.contains(pathSeparator)){
            javaClassPath = filePath.substring(0,filePath.indexOf(pathSeparator));
        }else if (javaClassPath.endsWith(JAR_SUFFIX)) {
            javaClassPath = filePath.substring(0, filePath.lastIndexOf(File.separator) + 1);
        }
        File serverLocalConfig = new File(javaClassPath);
        return serverLocalConfig.getAbsolutePath();
}

I have a file "cost.ini" on the root of my class path. My JAR file is named "cost.jar".

The following code:

  • If we have a JAR file, takes the directory where the JAR file is.
  • If we have *.class files, takes the directory of the root of classes.

try {
    //JDK11: replace "UTF-8" with UTF_8 and remove try-catch
    String rootPath = decode(getSystemResource("cost.ini").getPath()
            .replaceAll("(cost\\.jar!/)?cost\\.ini$|^(file\\:)?/", ""), "UTF-8");
    showMessageDialog(null, rootPath, "rootpath", WARNING_MESSAGE);
} catch(UnsupportedEncodingException e) {}

Path returned from .getPath() has the format:

  • In JAR: file:/C:/folder1/folder2/cost.jar!/cost.ini
  • In *.class: /C:/folder1/folder2/cost.ini

    Every use of File, leads on exception, if the application provided in JAR format.

  • Related