In this post, we will see how to retrieve the name of the operating system (OS) under use and current user home & directory information using Java code. In this particular example, we are using ‘System.getProperty’ to get OS name and User Home & Directory.

Simplest way to detect OS name, User home & directory information using Java ...!!! Share on X

NOTE:
I’ve tested the below code on Mac and Windows machines and based on that you’ll see the output as shown below. For Solaris & Linux, you’ll get the output accordingly.

Example

public class GetOSNameUserHomeAndDirectory {
	
	public static void main(String[] args) {
		
		String osName = System.getProperty("os.name");
		
		String currentUserDirectory = System.getProperty("user.dir");
		
		String userHome = System.getProperty("user.home");
				
		System.out.println("Operating System : "+osName);
		System.out.println("Current User Directory : "+currentUserDirectory);
		System.out.println("User Home : "+userHome);
	}
}

Output:

On Mac

Operating System: Mac OS X
User Directory : /Users/jradmin

On Windows

Operating System : Windows 10
Current User Directory : C:\Users\deepak\javasamples
User Home : C:\Users\deepak

That’s it, it’s that easy to retrieve OS name and User information using Java code. ?

Simplest way to detect OS name, User home & directory information using Java ...!!! Share on X

If you like this post , please check out my other useful blog posts on Rest Assured:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts