原帖的大多数人都回答如何检索在Java中运行进程的工作目录?
下面一个是我已经尝试与
String workingDirectory = "/home"; String command = "cd ../"; ProcessBuilder pb = new ProcessBuilder(new String[] { "cmd", "/c", command }); pb.directory(new File(workingDirectory)); pb.redirectErrorStream(true); Process process = pb.start(); // Some time later once the process has been closed workingDirectory = pb.directory().getAbsolutePath(); System.out.println("Path: " + workingDirectory);
这样做这是不行的,一旦它完成它出来与代码同样的 工作目录。
任何帮助将不胜感激,这将是一个非常有用的 认为知道。
更具体地说,我期待在Java中找到 动态创建的进程的工作目录,例如上面的代码片段。 这很重要,因为如上面预定义的命令, 工作目录有时可能会更改,我想将任何 更改保存到内存中供以后使用。
我找到了一种方法来做到这一点,似乎工作没有问题
这里是我正在处理输入的工作目录
public int osType = 1; // This is for Windows (0 is for Linux)
public boolean isValidPath(String path) {
try {
Paths.get(new File(path).getAbsolutePath());
} catch (InvalidPathException | NullPointerException ex) {
return false;
}
return true;
}
public String tracePath(String path) {
try {
if (!path.contains("%%") && !isValidPath(path)) return null;
if (path.contains("%%")) path = path.substring(path.indexOf("%%"));
int lastIndex = -1;
char filesystemSlash = ' ';
if (osType == 0)
filesystemSlash = '/';
if (osType == 1)
filesystemSlash = '\\';
if (osType == 0)
path = path.substring(path.indexOf(filesystemSlash));
if (osType == 1)
path = path.substring(path.indexOf(filesystemSlash) - 2);
String tmp = path;
boolean broken = true;
while (!isValidPath(tmp)) {
int index = tmp.lastIndexOf(filesystemSlash);
if (lastIndex == index) {
broken = false;
break;
}
tmp = tmp.substring(0, index);
lastIndex = index;
}
if (broken && lastIndex != -1) {
tmp = path.substring(0, lastIndex);
}
return tmp;
} catch (StringIndexOutOfBoundsException ex) {
return null;
}
}
这里是忽略的方法与路径问题(不使用它)
public boolean setDirectory(ProcessBuilder pb, String path) {
try {
pb.directory(new File(new File(path).getAbsolutePath()));
return true;
} catch (Exception ex) {
return false;
}
}
现在这里是怎么了开始为Windows或Linux的过程
File file = null;
if (osType == 1) {
ProcessBuilder pb = new ProcessBuilder(new String[] { "cmd", "/c", command + " & echo %% & cd" });
pb.redirectErrorStream(true);
if (!workingDirectory.equals(""))
setDirectory(pb, workingDirectory);
process = pb.start();
} else if (osType == 0) {
file = new File("script.sh");
FileWriter writer = new FileWriter(file, false);
writer.append(command + " && echo %% && pwd");
writer.flush();
writer.close();
ProcessBuilder pb = new ProcessBuilder(new String[] { "bash", System.getProperty("user.dir") + "/script.sh" });
pb.redirectErrorStream(true);
if (!workingDirectory.equals(""))
setDirectory(pb, workingDirectory);
process = pb.start();
} else
return;
最后这里是管理过程中的循环和工作目录
while (process.isAlive() || process.getInputStream().available() > 0) {
byte[] returnBytes = new byte[1024];
process.getInputStream().read(returnBytes);
char[] arr = new String(returnBytes).trim().toCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
char c = arr[i];
if (Character.isDefined(c))
sb.append(c);
}
String response = sb.toString();
if (!response.equals("")) {
String path = tracePath(response.trim().replace("\n", "").replace("\r", ""));
if (path != null && osType == 1) {
if (Paths.get(path).toFile().exists())
workingDirectory = path;
} else if (path != null && osType == 0) {
if (Paths.get(path).toFile().exists())
workingDirectory = path;
}
client.sendMessage(response + '\r' + '\n');
}
}
if (file != null) file.delete();
下面是从命令接收输出的网站
Connecting..
Connected.
Success. You have been connected -> Speentie
bash -c pwd
/root/hardsceneServer/remoteServer
%%
/root/hardsceneServer/remoteServer
bash -c cd ..
%%
/root/hardsceneServer
bash -c pwd
/root/hardsceneServer
%%
/root/hardsceneServer
bash -c dir
ircServer nohup.out remoteServer start.sh start1.sh start2.sh
%%
/root/hardsceneServer
bash -c cd ircServer
%%
/root/hardsceneServer/ircServer
bash -c dir
HardScene.jar hardscene_banned.properties start.sh
hardscene.properties nohup.out
%%
/root/hardsceneServer/ircServer
什么是命令吗? –
如果你问如何检索另一个进程的工作目录,那么这是不可能的。 – Andreas
当Linux可以做到这一点时,它是不可能的?我以为Java也可以与机器接口?必须有一种方法,即使它非常复杂。 – Speentie8081