Portable Directory Meanings in Java SE

Discussion in 'Java' started by Stefan Ram, Jul 14, 2008.

  1. Stefan Ram

    Stefan Ram Guest

    I would like to collect some opinions and information about
    the association between meanings of directory paths (i.e.,
    what are the directories intended to be used for?) and Java SE
    expressions for directory paths.

    This is intended to answer questions like: Which directory
    might be suggested to the user as a default for certain
    operations, like storing a document file that was just created
    and edited by the user?

    The answer to such a question should be a Java SE expression
    that gives an appropriate path under all possible environments
    (like, for example, Windows or Linux).

    So far, I have collected some Java expressions that can be
    printed with the program below.

    I would appreciate any comments about what others think that
    each of those paths could or should be used for by a Java
    application, even if someone only comments one or two paths,
    but not all of them.

    I also would like to know what this program prints on other
    systems (especially on a Linux System, but also different
    variants of Windows), but feel free to remove parts of the
    output to protect privacy or security.

    Of course, I also like to learn about other expressions that
    might evaluate to other directory paths with a certain meaning.

    A possible output of the program might look like:

    os.name =
    Windows 2000

    os.version =
    5.0

    java.home =
    C:\Program Files\JAVA\JRE1.7.0

    user.home =
    C:\WINDOWS

    user.dir =
    C:\dvl

    java.io.tmpdir =
    C:\WINDOWS\TEMP\

    fileSystemView.getDefaultDirectory() =
    C:\data
    C:\data

    fileSystemView.getHomeDirectory() =
    C:\WINDOWS\Desktop
    C:\WINDOWS\Desktop

    getCodeSource().getLocation() =
    C:\dvl
    C:\dvl


    class Main
    {
    public static void print( final java.lang.String string )
    { java.lang.System.out.println( string + " =" );
    java.lang.System.out.println( System.getProperty( string ));
    java.lang.System.out.println(); }

    public static void print( final java.io.File file )
    throws java.io.IOException
    { java.lang.System.out.println( file.getAbsolutePath() );
    java.lang.System.out.println( file.getCanonicalPath() );
    java.lang.System.out.println(); }

    public static void main( final java.lang.String[] args )
    throws java.lang.Exception
    {
    print( "os.name" );
    print( "os.version" );

    print( "java.home" );
    print( "user.home" );
    print( "user.dir" );
    print( "java.io.tmpdir" );

    final javax.swing.filechooser.FileSystemView fileSystemView =
    javax.swing.filechooser.FileSystemView.getFileSystemView();

    java.lang.System.out.println( "fileSystemView.getDefaultDirectory() =" );
    print( fileSystemView.getDefaultDirectory() );

    java.lang.System.out.println( "fileSystemView.getHomeDirectory() =" );
    print( fileSystemView.getHomeDirectory() );

    java.lang.System.out.println( "getCodeSource().getLocation() =" );
    print( new java.io.File( Main.class.getProtectionDomain().getCodeSource().
    getLocation().toURI() )); }}
    Stefan Ram, Jul 14, 2008
    #1
    1. Advertising

  2. On Mon, 14 Jul 2008 11:13:35 +0000, Stefan Ram wrote:

    > I also would like to know what this program prints on other
    > systems (especially on a Linux System, but also different
    > variants of Windows), but feel free to remove parts of the
    > output to protect privacy or security.
    >

    Here you go:

    $ java Main
    os.name =
    Linux

    os.version =
    2.6.25.9-40.fc8 # I'm running RedHat Fedora 8

    java.home =
    /home/java/jdk1.6.0_05/jre # with Sun JDK 1.6.05

    user.home = # My login directory when I ran it
    /home/kiwi

    user.dir = # My current working directory at the time
    /home/kiwi/compiler_tests/environ

    java.io.tmpdir = # default for FC8
    /tmp

    fileSystemView.getDefaultDirectory() = # $HOME (see below)
    /home/kiwi
    /home/kiwi

    fileSystemView.getHomeDirectory() = # $HOME (set when user logged in)
    /home/kiwi
    /home/kiwi

    getCodeSource().getLocation() = # As for user.dir
    /home/kiwi/compiler_tests/environ
    /home/kiwi/compiler_tests/environ

    Linux, like all Unices, has a number of additional defaults at both user
    and system level that we'd normally expect to be set for the particular
    host:

    - $PATH is where the OS looks for executables. It is typically something
    like:

    /bin:/usr/bin:/usr/local/bin

    which are (respectively) where os essentials, programs installed as part
    of the distro, and locally installed and/or written executables live.
    '.' (the current dir) isn't on the search path by default but is
    reasonably commonly added, together directories containing major
    3rd party packages such as Oracle.

    These defaults can be customised at both user and system level. The OS
    has facilities to make this easier and to let customisations survive
    OS reinstalls and upgrades.

    If the user is privileged, e.g. root, they will also have
    /sbin, /usr/sbin and possibly /usr/local/sbin in $PATH

    This shouldn't normally concern a Java application but its mentioned for
    completeness.

    - unlike Windows, you'd normally expect to find config files for
    standard packages in /etc and for locally installed/written packages
    in /usr/local/etc, possibly with user specific configurations in hidden
    files in $HOME. A 'hidden' file's name starts with '.' and isn't shown
    in a directory listing or a wild carded list unless explicitly asked
    for. This gives a little protection against accidental deletion etc.

    This does have implications for Java apps. You might reasonably expect a
    configurable Java app to search (in order) the current directory,
    $HOME, /usr/local/etc and /etc for its config file/properties file and
    use the first it found. Alternatively, if progressively localised
    customisations are are to be applied to a default config, it would be
    reasonable to do the search in the opposite order, and apply all files
    in the order they are found to build up a final configuration for the
    current user.

    I think the way /etc and its siblings is used is the major difference from
    Windows and its not really portable to another OS, except by coding the
    search into the app and using the os.name property to determine which
    search strategy should be used.

    One other point: many *nix utilities use a config file format that's
    compatible with a properties file. Comments start with #, individual
    parameters are written as "name = value".

    HTH


    --
    martin@ | Martin Gregorie
    gregorie. |
    org | Zappa fan & glider pilot
    Martin Gregorie, Jul 14, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    3
    Views:
    423
    John Harrison
    Sep 12, 2004
  2. Harvey Thomas

    RE: [OT] sentances with two meanings

    Harvey Thomas, Jul 15, 2003, in forum: Python
    Replies:
    45
    Views:
    1,110
    Paul Boddie
    Jul 23, 2003
  3. mangesh

    Meanings of macro

    mangesh, Oct 3, 2006, in forum: C++
    Replies:
    2
    Views:
    278
    Frederick Gotham
    Oct 3, 2006
  4. Felix Kater

    two meanings of a cast

    Felix Kater, Apr 27, 2007, in forum: C Programming
    Replies:
    18
    Views:
    566
    Army1987
    May 25, 2007
  5. Tim Couper
    Replies:
    1
    Views:
    413
    Marc 'BlackJack' Rintsch
    Sep 11, 2007
Loading...

Share This Page