原帖的大多數人都回答如何檢索在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