Link to home
Start Free TrialLog in
Avatar of petepalmer
petepalmer

asked on

Somewhere to save....

Hi all,
       I need to write a config file to an easy to find place. i first thought to just use c:\ but then remembered that my other machine doesn't have c: but f:  instead ( go figure ). anyway what i  was wondering was whether or not there is an easy way to find the system drive from Java?
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
On Windows e.g. that will return

     C:\Documents and Settings\<userid>

The drive is the system drive
Avatar of petepalmer
petepalmer

ASKER

That will do very nicely thank you :)
You're welcome.
Thanks for accepting
:°)
I like to keep my jars, config files and starter batch files in (almost) the same place. This code gives you the jar file path:

     String path;
     try
        {

            String className = getClass(  ).getName(  );
            className = className.substring( className.lastIndexOf( '.' ) + 1 )
                        + ".class";

            path = getClass(  ).getResource( className ).getPath(  );
            path = URLDecoder.decode( path, "UTF-8" );

            int pos = path.substring( 0, path.indexOf( '!' ) ).lastIndexOf( '/' );
            path = path.substring( path.indexOf( '/' ) + 1, pos );
            if( System.getProperty( "file.separator" ).equals( "/" ) )
            {
                //include "/" at start for UNIX OS
                path = "/" + path;
            }
        }
        catch( Exception ex )
        {
            System.out.println( "Could not init application paths; " + ex.getMessage(  ) );
        }
Wow, that was fast...