How to detect system drive using JAVA

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

Character.toString(System.getProperty("user.home").charAt(0));
0
 
ioantonCommented:
You can use:

File[] roots = File.listRoots();

to get the list of drives on your system
0
 
MaranellasAuthor Commented:
I don't need to get list of drives.I need to get main drive where windows installed.
0
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
http://www.bigresource.com/Tracker/Track-vb-NkqegKV5n3/
http://leepoint.net/notes-java/io/30properties_and_preferences/40sysprops/10sysprop.html
0
 
ioantonCommented:
Another approach:

System.out.println(System.getProperty("user.home").charAt(0));

Assuming the user home directory is always an the windows drive.
0
 
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
0
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.