Click or drag to resize

Set host working directory

Problem

You want to set the working directory of the host.

Solution

To set the host's working directory, set WorkingDirectory and pass the path of the desired working directory. The string will be resolved by expanding the string as an environment variable. For instance, "%TEMP%" will resolve to the full temporary directory path. The task will fail if the directory does not exist on the agent machine.

Java
package stkparallelcomputingserversdk.howto;

import agi.parallel.client.ClusterJobScheduler;
import agi.parallel.client.IJobScheduler;
import agi.parallel.client.Job;
import agi.parallel.infrastructure.Task;

public class SetHostWorkingDirectory {
    public static void main(String[] args) {
        try (IJobScheduler scheduler = new ClusterJobScheduler("localhost")) {
            scheduler.connect();

            Job job = scheduler.createJob();
            // Use the Windows variables to specify specific directories.
            job.getTaskEnvironment().setWorkingDirectory("%TEMP%");

            job.addTask(new ReturnsWorkingDirectory());

            job.submit();
            job.waitUntilDone();

            System.out.println("Working Directory: " + job.getTasks().get(0).getResult());
        }

        /*
         * The output of the application should resemble:
         * Working Directory: C:\Users\jdoe\AppData\Local\Temp
         */
    }

    private static class ReturnsWorkingDirectory extends Task {
        @Override
        public void execute() {
            this.setResult(System.getProperty("user.dir"));
        }
    }
}
Discussion

By default the working directory of the host process is the agent bin folder. There are some cases where a task needs a user defined working directory. The most common of these cases is that the application assumes a relative path to data. Sometimes this can be avoided by modifying the application itself. However, setting the working directory is an option in cases where modifying the application is not possible.

See Also

Other Resources

STK Parallel Computing Server 2.9 API for Java