I need to run my own process from Java inside the symlink directory. Let the following directory structure:
guido@Firefly:~/work$ tree
.
└── path
└── to
└── symlink -> /home/guido/work
3 directories, 0 files
I am starting a Java application from a directory ~/workand want to start my own process in the directory ~/work/path/to/symlink.
However, if I use the following Java code, the symlink working directory resolves the real path . Instead, I would like to run the command in an absolute way .
(Please note that the command pwdis for illustration purposes only and should be replaced with a “real” one (for example, go buildin my case)).
File baseDir = new File("/home/guido/work");
File link = new File(baseDir, "path/to/symlink");
Files.createSymbolicLink(link.toPath(), baseDir.toPath());
Process process = new ProcessBuilder()
.command("pwd")
.directory(link)
.start();
String output = getOutput(process);
System.out.println(output);
, , . , .
String[] cmd = {"/bin/sh", "-c", "cd " + link + " && pwd"};
Process process = Runtime.getRuntime().exec(cmd);
String output = getOutput(process);
System.out.println(output);
unit test gist .