0

Is there already an existing object/method able to tell us on which drive our running Java Application is installed ?

Thanks.

Farid

Farid
  • 31
  • 1
  • 3

4 Answers4

4

Try the following on Windows:

File file = new File(".").getAbsoluteFile();
File root = file.getParentFile();
while (root.getParentFile() != null) {
    root = root.getParentFile();
}
System.out.println("Drive is: "+root.getPath());

On Unix systems, this will be less useful as the root will always be /

Kevin
  • 30,111
  • 9
  • 76
  • 83
0

Check this example: http://littletutorials.com/2008/03/10/getting-file-system-details-in-java/

On Windows environment you could read the HOMEDRIVE variable. If you run this code you will see all the environment variables available.

import java.util.Map;

public class EnvMap {
    public static void main (String[] args) {
        Map<String, String> env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.format("%s=%s%n", envName, env.get(envName));
        }
    }
}

HOMEDRIVE doesn't exist as a concept in Unix environments.

JuanZe
  • 8,007
  • 44
  • 58
0

According to the java.io.File javadoc:

The parent of an abstract pathname may be obtained by invoking the getParent() method of this class and consists of the pathname's prefix and each name in the pathname's name sequence except for the last. Each directory's absolute pathname is an ancestor of any File object with an absolute abstract pathname which begins with the directory's absolute pathname. For example, the directory denoted by the abstract pathname "/usr" is an ancestor of the directory denoted by the pathname "/usr/local/bin".

The prefix concept is used to handle root directories on UNIX platforms, and drive specifiers, root directories and UNC pathnames on Microsoft Windows platforms, as follows:

  • For UNIX platforms, the prefix of an absolute pathname is always "/". Relative pathnames have no prefix. The abstract pathname denoting the root directory has the prefix "/" and an empty name sequence.

  • For Microsoft Windows platforms, the prefix of a pathname that contains a drive specifier consists of the drive letter followed by ":" and possibly followed by "\\" if the pathname is absolute. The prefix of a UNC pathname is "\\\\"; the hostname and the share name are the first two names in the name sequence. A relative pathname that does not specify a drive has no prefix.

So a solution might be to create a relative File object (use "." for the current directory), get the parent and extract the drive letter. This is of course not portable.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
0

If you want to know the current working directory of the running system, you use the following:

String currentDirectory = System.getProperty("user.dir");

This will tell you the directory it was started in, which is often where the Jar file is located, or the root of the classpath is located.

Edit: The System Class javadoc for the method getProperties() has the common set of System properties that are available.

aperkins
  • 12,914
  • 4
  • 29
  • 34