Search This Blog

Showing posts with label working directory. Show all posts
Showing posts with label working directory. Show all posts

How to Get User Related Informations in Java

In java, there are some methods that will help you to know some user related informations. Here also,

System.getProperty() 
method can give some informations about the user like:

User account name
User Working directory
User Home directory

To get the username of current user on the computer just call

System.getProperty("user.name")

It will return the current username as a string.


To get the working directory of current user on the computer call

System.getProperty("user.dir")

It will return the path of working directory of current user as a string.


To get the home directory (user's home folder) of current user on the computer call

System.getProperty("user.home")

It will return the path to home directory (folder) of current user as a string.

How to Use System.getProperty() - Keys for System Properties

There is a method getProperty() in System class in java. As the name indicates, this java method is used to get System specific, operating system specific or user specific properties. There are different keys which are strings passed as parameter to the getProperty() method.

Usage

The following sample java code will show you how to use the getProperty method in System class. The key is a string. The System.getProperty() method takes a string parameter (argument). Based on the string passed, the method System.getProperty() returns different properties as string. In this example,we pass "java.io.tmpdir" as the parameter, which is the key to get the temporary directory (temp directory) for the current user account.

String property = "java.io.tmpdir"; String tempDir = System.getProperty(property); System.out.println("temp directory ="+ tempDir );


Similarly, for each property, there is a key. Below, the keys for different properties are listed.
"file.separator"
Gives the character that separates components of a file path. This is "/" on UNIX and "\" on Windows. This character is used in File paths for cross platform java applications
"java.class.path"
Returns the path used to find directories and JAR archives containing class files. Elements of the class path are separated by a platform-specific character specified in the path.separator property.
"java.home"
Returns the installation directory for Java Runtime Environment (JRE)
"java.vendor"
Returns the name of JRE vendor
"java.vendor.url"
Returns the URL of JRE vendor
"java.version"
Returns the version number of JRE
"line.separator"
Returns the character sequence used by operating system to separate lines in text files. This is operating system specific. It can be '\n', '\r' or "\r\n".
"os.arch"
Returns the operating system architecture
"os.name"
Returns the name of the Operating system
"os.version"
Returns the version of the Opposing system.
"path.separator"
Returns path separator character used in java.class.path
"user.dir"
Returns the path to the working directory of current user in the computer.
"user.home"
Returns the path to the home directory of current user.
"user.name"
Returns the name of current user (user account name)