public inbox for java@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: user.dir property
  2000-04-01  0:00 ` user.dir property Kresten Krab Thorup
@ 2000-04-01  0:00   ` Jeff Sturm
  2000-04-01  0:00     ` Godmar Back
  0 siblings, 1 reply; 3+ messages in thread
From: Jeff Sturm @ 2000-04-01  0:00 UTC (permalink / raw)
  To: Kresten Krab Thorup; +Cc: java-discuss

Kresten Krab Thorup wrote:
> I'm trying to set the property user.dir and thereby effectively cd'ing
> to a given directory.  The following piece of code tries do do that, but
> fails on JDK1.2.2.  The code does the following
> 
> create file "/tmp/foo.bar"
> set user.dir = "/tmp"
> read "foo.bar"
> 
> Obviously, this is only interesting if the original value of "user.dir"
> is different from "/tmp".
> 
> The question is if this ought to be possible. As I read the javadoc for
> java.lang.File, it seems that it should.

I don't think there is a way to change the current directory of a Java
VM, at least without resorting to native code.

System properties are set by the VM during initialization, and
apparently never re-examined.  That's how libgcj works, and how Sun's
JDK empirically seems to work.  Effectively, system properties are
read-only.  It is probably a bug in the Java core API that they can be
changed by user code at all.

I remember a discussion years ago on advanced-java about this very
topic.  One of the Sun engineers suggested at the time that chdir() was
not supported in JDK 1.0 because its behavior is a little unpredictable
in a multithreaded environment.  In some thread models a chdir() would
affect all VM threads, in others it would affect only the calling
thread.  I suppose not supporting chdir() at all is an easy way to avoid
the problem...

--
Jeff Sturm
jsturm@sigma6.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: user.dir property
  2000-04-01  0:00   ` Jeff Sturm
@ 2000-04-01  0:00     ` Godmar Back
  0 siblings, 0 replies; 3+ messages in thread
From: Godmar Back @ 2000-04-01  0:00 UTC (permalink / raw)
  To: Jeff Sturm; +Cc: Kresten Krab Thorup, java-discuss

Kresten, regarding chdir(), place your vote at:
http://developer.java.sun.com/developer/bugParade/bugs/4045688.html
Somewhat related is also:
http://developer.java.sun.com/developer/bugParade/bugs/4156278.html

As an aside, note that Mac's MRJ is said to honor changes in user.dir.
In my own project, I also honor it and I allow for a new user.dir
property to be passed to new processes.

Regarding Jeff's comment that 
> System properties are set by the VM during initialization, and
> apparently never re-examined.

I believe this depends on the specific property.  user.dir is not
reexamined because of the threading concerns you mentioned.  But
others are:  for instance, http.proxyHost and http.proxyPort are
examined every time a http URL is opened (as of 1.2).  This allows
an application to prompt a user for their firewall/proxy host.  Of 
course, this is just a consequence of the (bad) decision to use user 
properties to specify the http proxy, but I think it shows that 
there are no "rules" as far as properties are concerned.  Sun's just 
making it up as they go.

	- Godmar

^ permalink raw reply	[flat|nested] 3+ messages in thread

* user.dir property
       [not found] <20000310103001.7995.qmail@sourceware.cygnus.com>
@ 2000-04-01  0:00 ` Kresten Krab Thorup
  2000-04-01  0:00   ` Jeff Sturm
  0 siblings, 1 reply; 3+ messages in thread
From: Kresten Krab Thorup @ 2000-04-01  0:00 UTC (permalink / raw)
  To: java-discuss

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1913 bytes --]

Here's a question.  

I'm trying to set the property user.dir and thereby effectively cd'ing
to a given directory.  The following piece of code tries do do that, but
fails on JDK1.2.2.  The code does the following

create file "/tmp/foo.bar"
set user.dir = "/tmp"
read "foo.bar"

Obviously, this is only interesting if the original value of "user.dir"
is different from "/tmp".

The question is if this ought to be possible. As I read the javadoc for
java.lang.File, it seems that it should.

Any coments?

--Kresten

----------------------------------------------------------------------
import java.io.*;

public class TestUserDir
{
  public static void main (String[] args)
    throws IOException
  {
    String user_dir = System.getProperty ("user.dir");
    System.out.println ("user.dir = "+user_dir);

    File tmp = File.createTempFile ("foo", "bar");
    System.out.println ("tmpfile  = "+tmp);

    File tmp2 = new File(tmp.getAbsolutePath ());
    System.out.println ("abstmp   = "+tmp2);

    String parent = tmp2.getParent ();
    String file   = tmp2.getName ();

    System.out.println ("parent   = "+parent);
    System.out.println ("file     = "+file);

    try {
      FileOutputStream fo = new FileOutputStream (tmp2);
      fo.write (27);
      fo.close ();
    } finally {
      System.out.println ("wrote one byte to "+tmp2);
    }

    System.setProperty ("user.dir", parent);
    System.out.println ("cd'ed to "+parent);

    try {
      FileInputStream fi = new FileInputStream (file);
      int b = fi.read ();
      if (b == 27)
	{
	  System.out.println ("sucessfully read from "+file);
	}
    } catch (IOException e) {
      System.out.println ("** failed to read "+file);
      throw e;
    }
  }
}



-- 
Kresten Krab Thorup, Director of Research
Eastfork Object Space (EOS), Margrethepladsen 3, 8000  Ã…rhus C, Denmark
Tel: +45 8732 8787 / Fax: +45 8732 8788 / Mob: +45 2343 4626

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2000-04-01  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20000310103001.7995.qmail@sourceware.cygnus.com>
2000-04-01  0:00 ` user.dir property Kresten Krab Thorup
2000-04-01  0:00   ` Jeff Sturm
2000-04-01  0:00     ` Godmar Back

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).