How to detect system drive using JAVA

As the topic says.I need short function to detect system drive letter.
Who is Participating?
ioantonConnect With a Mentor Commented:
Indeed, the returned value is of type char. But you can convert it to string using:

You can use:

File[] roots = File.listRoots();

to get the list of drives on your system
MaranellasAuthor Commented:
I don't need to get list of drives.I need to get main drive where windows installed.
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Gurvinder Pal SinghCommented:
try the following code

String systemDrive = System.getProperty("SystemDrive");

see the following link for reference
Another approach:


Assuming the user home directory is always an the windows drive.
MaranellasAuthor Commented:
okay when i try

String systemDrive = System.getProperty("SystemDrive");

i get NULL response

and when i try System.out.println(System.getProperty("user.home").charAt(0));

i get the following error

found   : char
required: java.lang.String
        private String drv = System.getProperty("user.home").charAt(0);

but java.lang package is imported already
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.