Portable Directory Meanings in Java SE

S

Stefan Ram

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() )); }}
 
M

Martin Gregorie

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top