Skip to main content

what do we do with chdir()?

3 replies [Last post]
vpatryshev
Offline
Joined: 2004-06-30
Points: 0

This drags from 1997... see http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4045688

<br />
    String rootname = "D:\\tmp";<br />
    String filename = "testfiles.txt";<br />
    String rootfilename = rootname + File.separator + filename;<br />
    String expected = "this is a test";<br />
    writeToFile(expected, rootfilename);<br />
    System.setProperty("user.dir", rootname);<br />
    File file = new File(filename);<br />
    assertEquals(rootfilename, file.getAbsolutePath());<br />
    File rootfile = file.getAbsoluteFile();<br />
    assertTrue(rootfile.exists());<br />
    assertTrue(file.exists());<br />

Is not it funny that the first two asserts pass, and the third one fails?

Someone said this:

"If you need to simulate chdir, it is trivial to do:
System.setProperty("user.dir", newdir);
//...
File file = new File(filename).getAbsoluteFile();" - is not it funny that changing "user.dir" leads to this kind of discrepancy? They say chdir() is a hack. It is an operation used by millions in thousands of programs. Using a surrogate "user.dir" instead is, I think, a worse hack.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mthornton
Offline
Joined: 2003-06-10
Points: 0

In my opinion the best way to resolve this is to make user.dir a read only property. Changing the current directory in a system with multi threading and lazy loading of resources (classes and DLLs), and where the search path for DLLs depends on the current directory, is likely to lead to surprising and hard to debug behaviour. Even if your own code is single threaded, the JVM itself uses multiple threads.

vpatryshev
Offline
Joined: 2004-06-30
Points: 0

:) Come on. Are you serious? Why do you think it is okay to switch current directory in a c program, but not in a java program? Are java program second class citisens, just because there are threads in java programs?

Java is a language as good as any other, even better. People want to write applications in Java. If, on the other hand, you want to restrict control, you are free to use Java security model.

Now, there are other features. How about disabling setting System properties, just because there are other threads? How about redirecting System.out?

evrignaud
Offline
Joined: 2005-01-23
Points: 0

You can use chdir() carefully if you know what you are doing. It's sure that in a multithreaded environment it can be very tricky.
An easy way to implement chdir is using the jna library (https://github.com/twall/jna) like this:
import com.sun.jna.NativeLibrary;
import com.sun.jna.Platform;
/**
* Call native low level Posix functions.
*/
public class Posix
{
private static final int MAX_PATH = 1024;
private NativeLibrary cLib = null;
public void chdir(String dirName)
{
int error = getCLib().getFunction(getOsDependentFuntionName("chdir")).invokeInt(
new Object[] { dirName });
if (error != 0)
throw new Error("Could not change working directory to: " + dirName);
String cwd = getcwd();
// Change the current directory property used by the java VM
System.setProperty("user.dir", cwd);
}
private NativeLibrary getCLib()
{
if (cLib == null)
cLib = NativeLibrary.getInstance(null); // Will load libcxxx.so or msvcrt.dll
return cLib;
}
public String getcwd()
{
String cwd = getCLib().getFunction(getOsDependentFuntionName("getcwd")).invokeString(
new Object[] { new String(new byte[MAX_PATH]), MAX_PATH }, false);
return cwd;
}
private String getOsDependentFuntionName(String fname)
{
if (Platform.isWindows())
return "_" + fname;
return fname;
}
}