Changing the Current Working Directory in Java

Changing the current working directory in Java?

There is no reliable way to do this in pure Java. Setting the user.dir property via System.setProperty() or java -Duser.dir=... does seem to affect subsequent creations of Files, but not e.g. FileOutputStreams.

The File(String parent, String child) constructor can help if you build up your directory path separately from your file path, allowing easier swapping.

An alternative is to set up a script to run Java from a different directory, or use JNI native code as suggested below.

The relevant OpenJDK bug was closed in 2008 as "will not fix".

Change the Current Working Directory in Java

I'm pretty sure you can't modify the current process' working directory. Instead you can use the File(String, String) constructor which creates a new File instance from a parent pathname string and a child pathname string.

Java Change File Working Directory

Updating my answer, since VolkerSeibt pointed out that it was incorrect. Good catch.

This is possible through System.setProperty. You can change the current working directory by changing the "user.dir" system property:

System.setProperty("user.dir", "/foo/bar");

See http://www.javacodex.com/Files/Set-The-Current-Working-Directory for further explanation.

How to change current directory in JAVA?

You cannot re-assign the default working directory of your process - it is given to your program at JVM's start-up, and does not change throughout the lifetime of the program.

In order to evaluate a relative path, construct an absolute path from the path of the origin (the XML file), a file path separator, and the relative path:

String xmlFilePath = "c:\\temp\\xml\\my_file.xml";
String relativePath = "..\\resources\\file.ico";
String resourcePath = "c:\\temp\\xml\\..\\resources\\file.ico";

Java will interpret paths like that as "c:\\temp\\resources\\file.ico".

changing the working-directory of command from java

To implement this you can use the ProcessBuilder class, here's how it would look like:

File pathToExecutable = new File( "resources/external.exe" );
ProcessBuilder builder = new ProcessBuilder( pathToExecutable.getAbsolutePath(), "-i", "input", "-o", "output");
builder.directory( new File( "resources" ).getAbsoluteFile() ); // this is where you set the root folder for the executable to run with
builder.redirectErrorStream(true);
Process process = builder.start();

Scanner s = new Scanner(process.getInputStream());
StringBuilder text = new StringBuilder();
while (s.hasNextLine()) {
text.append(s.nextLine());
text.append("\n");
}
s.close();

int result = process.waitFor();

System.out.printf( "Process exited with result %d and output %s%n", result, text );

It's quite a bunch of code, but gives you even more control on how the process is going to be run.

Achieving the effect of changing the Java working directory

A very alternative way to change the CWD of the library would be to launch it in a different Java process, for which you can specify CWD at launch time (see for example ProcessBuilder documentation). If I understand the problem correctly, your current flow is somewhat similar to

 launch program with CWD 'a'
use library X that expects CWD to be 'b' // <-- problem here

The new flow would be

 launch program with CWD 'a'
determine desired CWD for launching library X
launch wrapper for library X with CWD 'b'
internally, library X is happy because its CWD is as expected

Of course, this will force you to write a full wrapper, and communicate it using sockets&serialization, or any other communication strategy of your choice. On the plus side, this will allow you to launch several instances of your library side-by-side, without their CWDs interfering with each other -- at the cost of JVM and communication overheads.



Related Topics



Leave a reply



Submit